Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Day 03 Introduction to C. 2 Memory layout and addresses 5 10 12.5 9. 8 r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; 430043044308431243164317.

Similar presentations


Presentation on theme: "1 Day 03 Introduction to C. 2 Memory layout and addresses 5 10 12.5 9. 8 r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; 430043044308431243164317."— Presentation transcript:

1 1 Day 03 Introduction to C

2 2 Memory layout and addresses 5 10 12.5 9. 8 r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; 430043044308431243164317 x y f g c d

3 3 Pointers made easy - 1 ? f 4300 float f; // data variable - holds a float ? f_addr 4304 float *f_addr; // pointer variable – holds an address to a //float f_addr = &f; // & = address operator NULL 4300

4 4 *f_addr = 3.2; // indirection operator or dereferencing f f_addr 43004304 4300 3.2 g 4308 float g=*f_addr; // indirection: g is now 3.2 f = 1.3; 3.2 1.3

5 5 Pointer operations Creation int *ptr; Pointer assignment/initialization ptr = &i; (where i is an int and &i is the address of i) ptr = iPtr; (where iPtr is a pointer to an int) Pointer indirection or dereferencing i = *ptr; *ptr = 5; (i is an int and *ptr is the int value pointed to by ptr)

6 Pointers and arrays int p[7], *ptr; // Both p and ptr are pointers // i.e. hold addresses. // p is already pointing to a fixed location and // cannot be changed. // ptr is still to be initialized. p[i] is an int value. p, &p[i] and (p+i) are addresses or pointers. *p = 5;  p[0] = 5; *(p+i) = 5;  p[i] = 5; Label p[0]p[1]p[2]p[3]p[4]p[5]p[6]ptr Value 243420288925617? Address 2000200420082012201620202024…2032

7 int *ptr; int p[7]; ….. ptr = p; // or ptr = &p[0] ptr +=2; What is the value of ptr? ptr = ptr + 2 * sizeof(int) = ptr+8 bytes ptr = 2000 + 8 = 2008 => ptr = &(p[2]); Pointer arithmetic p[0]p[1]p[2]p[3]p[4]p[5]p[6]ptr 243420288925617? Address 2000200420082012201620202024…2032 ERROR: p = ptr; because “p” is a constant address, points to the beginning of a static array. 20002008

8 #include int main() { int *ptr; /* allocate space to hold 4 ints */ ptr = (int*)malloc(4 * sizeof(int)); /* do stuff with the data */ *ptr=4; //ptr[0] = 4; /* free up the allocated space */ free(ptr); return 0; } Dynamic memory allocation Explicit allocation and de-allocation by user using malloc() and free(). (void *)malloc(size_t size); void free(void *ptr); bytes sizeof(type)

9 int *ptr; ? ptr 4000 ptr = (int*)malloc(4 * sizeof(int)); //Address 6000 on the heap is allocated 6000600460086012 ???? 6000 *ptr=4; 4 free(ptr);

10 Man pages – Section 3 >>man –s 3 free >>man –s 3 malloc

11 Memory allocation for a process. Code segment Static data segment Dynamic data segment (heap) Stack malloc looks for space on the heap int *p; //p is created in the static data segment p = (int *)malloc(4 * sizeof(int)); //Space for 4 ints i.e. contiguous 16 //bytes is allocated on the heap

12 Dynamic array int *ptr, i, count; printf(“Enter the number of items in the array”); scanf(“%d”,&count) //Create a dynamic array of “size” ints. ptr = (int*)malloc( count * sizeof(int) ); for(i=0; i<count; i++){ ptr[i] = i; } count * sizeof(int)

13 Array of Pointers Variable length strings char *card[4]; // card[4] => array of 4 elements //char* => element is a pointer to a character. // card[4] => array of 4 char pointers 4000card[0] 4004card[1] 4008card[2] 4012card[3]

14 card[0] = (char*)malloc(6*sizeof(char)); card[1] = (char*)malloc(3*sizeof(char)); and so on. Static allocation of a 2D array: char card[4][10]; //waste of space

15 15 Common errors - Memory leak int *ptr, x; ptr = (int*)malloc(10*sizeof(int)); //ptr gets space //starting at address 3000 ptr = &x; The space allocated through malloc is no longer available for use by the program. Released only when program quits. Becomes a problem in large programs where a large number of variables are created and destroyed during the execution of the program.

16 16 Common erros - Dangling pointers int *i, *x; i = (int*)malloc( 5 x sizeof(int)); x = i; / * both point to the same address. */ free(x); /* both i and x are dangling pointers and trying to access either of them can cause logical errors */ x = NULL; /* One way to prevent incorrect access */ i = NULL; void free_ptr(void *ptr){ free(ptr); ptr = NULL; }


Download ppt "1 Day 03 Introduction to C. 2 Memory layout and addresses 5 10 12.5 9. 8 r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’; 430043044308431243164317."

Similar presentations


Ads by Google