230 likes | 367 Views
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.
E N D
UBC104Embedded 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 UBC104 Embedded Systems
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
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
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
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
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
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
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
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
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
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
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
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
Overview • Structures • Types • Type casting • Memory allocation • Linked list UBC104 Embedded Systems
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
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
Types • Defines a new type (added to native types) • typedef <type-name><type>; • Example: typedef struct point point; point p; UBC104 Embedded Systems
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
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
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
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
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