Presentation is loading. Please wait.

Presentation is loading. Please wait.

UBC104 Embedded Systems Variables, Structures & Pointers.

Similar presentations


Presentation on theme: "UBC104 Embedded Systems Variables, Structures & Pointers."— Presentation transcript:

1 UBC104 Embedded Systems Variables, Structures & Pointers

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

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

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

5 UBC104 Embedded Systems 5 Basic Types char8 bits [-128;127] short16 bits[-32768;32767] long32 bits[-2 31 ;2 31 -1] int32 (or 16) bits[-2 31 ;2 31 -1] float32 bitsapprox. 10 -38 ;10 38 double64 bitsditto

6 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;

7 UBC104 Embedded Systems 7 Strings Strings are a set of characters terminated by a “0” character H0x00001000 e0x00001001 l0x00001002 l0x00001003 o0x00001004 \00x00001005 char *str = “Hello”;

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

9 UBC104 Embedded Systems 9 Hups, we’ve used pointers H0x00001000 e0x00001001 l0x00001002 l0x00001003 o0x00001004 \00x00001005 char *str = “Hello”; 0x00000A000x00001000 str

10 UBC104 Embedded Systems 10 Pointing to an integer #include void main() { int number; int *nptr; number = 10*15; nptr= &number; } 0x00000A000x00x00000A040x0 0x00000A00150 0x00000A040x00000A00

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

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

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

14 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;

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

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

17 UBC104 Embedded Systems 17 Structures (cont.) struct { ; … ; } = { value_1, …, value_n };

18 UBC104 Embedded Systems 18 Types Defines a new type (added to native types) typedef ; Example: typedef struct point point; point p;

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

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

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

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

23 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


Download ppt "UBC104 Embedded Systems Variables, Structures & Pointers."

Similar presentations


Ads by Google