1 / 23

UBC104 Embedded Systems

UBC104 Embedded Systems. Variables, Structures & Pointers. Memory. 0x0000000. 0x0001000. Task for Today: Understanding that memory is accessed by addresses mov 0x1, 0x0001323  set contents that is specified by address to 0x1. 3. 0x0002000. 0x0003000. 0x0003FFF. 0x00. 0xFF.

lara
Download Presentation

UBC104 Embedded Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UBC104Embedded Systems Variables, Structures & Pointers

  2. Memory 0x0000000 0x0001000 • Task for Today: Understanding that memory is accessed by addresses • mov 0x1, 0x0001323  set contents that is specified by address to 0x1 3 0x0002000 0x0003000 0x0003FFF 0x00 0xFF UBC104 Embedded Systems

  3. Variables • Name for a memory address int a= 3; a= 4; mov 4, 0x000053A4 0x00000000 0x00001000 0x00002000 0x00003000 0x00004000 a 3 0x00005000 (0x000053A4) 0x00006000 0x00007000 UBC104 Embedded Systems

  4. C into Assembler 1 int a= 3; 2 3 int main(int argc, char** argv) { 4 5 a= 4; 6 7 return 0; 8 9 } <main+0>: push %ebp <main+1>: mov %esp,%ebp <main+3>: sub $0x8,%esp <main+6>: and $0xfffffff0,%esp <main+9>: mov $0x0,%eax <main+14>: sub %eax,%esp <main+16>: movl $0x4,0x80493cc <main+26>: leave <main+27>: ret UBC104 Embedded Systems

  5. Basic Types • char 8 bits [-128;127] • short 16 bits [-32768;32767] • long 32 bits [-231;231-1] • int 32 (or 16) bits [-231;231-1] • float 32 bits approx. 10-38;1038 • double 64 bits ditto UBC104 Embedded Systems

  6. Variable Declarations unsigned int Number; char c; double pi = 3.14159; float this_is_a_very_long_name; int n1, n2, n3; Some invalid declarations: int 7Sons; float wrong-identifier; short #name; double int; UBC104 Embedded Systems

  7. 0x00001000 H 0x00001001 e 0x00001002 l 0x00001003 l 0x00001004 o 0x00001005 \0 Strings • Strings are a set of characters terminated by a “0” character char *str = “Hello”; UBC104 Embedded Systems

  8. Printing strings #include <stdio.h> void main() { int number; char *format = ”The result is %d.\n“; number = 33*77; printf(format, number); } UBC104 Embedded Systems

  9. Hups, we’ve used pointers char *str = “Hello”; str 0x00000A00 0x00001000 0x00001000 H 0x00001001 e 0x00001002 l 0x00001003 l 0x00001004 o 0x00001005 \0 UBC104 Embedded Systems

  10. 0x00000A00 0x00000A04 0x0 0x0 Pointing to an integer #include <stdio.h> void main() { int number; int *nptr; number = 10*15; nptr= &number; } 0x00000A00 150 0x00000A04 0x00000A00 UBC104 Embedded Systems

  11. Printing the pointer #include <stdio.h> void main() { int number; int *nptr; number = 10*15; nptr= &number; printf(“Number: %d\n”, nptr); } what would this print? UBC104 Embedded Systems

  12. Printing the contents #include <stdio.h> void main() { int number; int *nptr; number = 10*15; nptr= &number; printf(“Number: %d\n”, *nptr); } Dereference!!! UBC104 Embedded Systems

  13. Pointer Arithmetic #include <stdio.h> void main() { int number; int *nptr; number = 10*15; nptr= &number; nptr++; nptr++; nptr++; printf(“Number: %d\n”, *nptr); } UBC104 Embedded Systems

  14. Summary for Pointers • Declaration: type *variablename; e.g.: int *nptr; • Assignment of address: pointer= &othertype; e.g.: nptr= &number; • Dereference: othertype= *pointer; e.g.: number= *nptr; UBC104 Embedded Systems

  15. Overview • Structures • Types • Type casting • Memory allocation • Linked list UBC104 Embedded Systems

  16. 0x00001000 10 0x00001004 15 0x00001008 … 0x0000100C … 0x00001010 … 0x00001014 … Structures • Structures are a collection of variables struct point { int x; int y; }; struct point p= {10, 15}; UBC104 Embedded Systems

  17. Structures (cont.) struct <struct-name> { <type-name_1> <variable-name_1>; … <type-name_n> <variable-name_n>; } <variable_name> = {value_1, …, value_n}; UBC104 Embedded Systems

  18. Types • Defines a new type (added to native types) • typedef <type-name><type>; • Example: typedef struct point point; point p; UBC104 Embedded Systems

  19. Access to elements • Two ways to access elements: • <variable-name>.<element-name> • <pointer-name>-><element-name> • Examples: p.x= 10; struct point *ptr; ptr= &p; printf(“x-coordinate: %d\n”,ptr->x); UBC104 Embedded Systems

  20. Type casting • void* is a general pointer; void* p= 0xABCD1234; int x= ((struct point *) p)->x; • (<type>) <variable_name> • Example: printf (“point(%d, %d)”, ((struct point *) p)->x, ((struct point *) p)->y); UBC104 Embedded Systems

  21. Memory allocation • malloc reserves given number of bytes • Defined as: char *malloc(int size); • Example: #include <malloc.h> struct point *ptr; ptr= (struct point *) malloc(sizeof(struct point)); if (ptr==NULL) {exit(-1);} p->x= 1; p->y= 1; UBC104 Embedded Systems

  22. De-allocation • free releases memory associated with a pointer • Defined as: char *free(void *); • Example: #include <malloc.h> struct point *ptr; ptr= (struct point *) malloc(sizeof(struct point)); if (ptr==NULL) {exit(-1);} free(ptr); UBC104 Embedded Systems

  23. Summary • &var returns the address of a variable “var” • *ptr returns the contents of the memory location “ptr” points to • Variables are used as memory references • Pointers provide another form of references • Space for variables is handled automatically • Space for pointers has to be allocated & freed manually • Structures are additional information for the compiler to arrange memory accesses UBC104 Embedded Systems

More Related