Day 02 Introduction to CSlide 2Pointers made easy - 1Slide 4Pointer operationsPointers and arraysSlide 7Slide 8Slide 9Man pages – Section 3Memory allocation for a process.Dynamic arrayArray of PointersSlide 14Common errors - Memory leakCommon erros - Dangling pointers1Day 02Introduction to C2Memory layout and addresses 5 10 12.5 9. 8 r sint x = 5, y = 10;float f = 12.5, g = 9.8;char c = ‘r’, d = ‘s’;4300 4304 4308 4312 4316 4317 x y f g c d33Pointers made easy - 1Pointers made easy - 1?f4300float f; // data variable - holds a float?f_addr4304float *f_addr; // pointer variable – holds an address to a //floatf_addr = &f; // & = address operatorNULL 430044*f_addr = 3.2; // indirection operator or dereferencing ff_addr4300 430443003.2g4308float g=*f_addr; // indirection: g is now 3.2 f = 1.3;3.21.35Pointer operationsCreationint *ptr; Pointer assignment/initializationptr = &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 dereferencingi = *ptr; (i is an int and *ptr is the int value pointed to by ptr)Pointers and arraysint 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; p[0] p[1] p[2] p[3] p[4] p[5] p[6] ptr 24 34 202 88 92 56 17 ?Address2000 2004 2008 2012 2016 2020 2024 … 2032int *ptr;int p[10];ptr = p; // or ptr = &p[0]ptr +=2; //Assume ptr = 2000What is the value of ptr?ptr = ptr + 2 * sizeof(int) = ptr+8 bytesptr = 2000 + 8 = 2008=> ptr = &(p[2]);Pointer arithmeticp[0] p[1] p[2] p[3] p[4] p[5] p[6] ptr 24 34 202 88 92 56 17 ?Address2000 2004 2008 2012 2016 2020 2024 … 2032ERROR: p = ptr; because “p” is a constant address, points to the beginning of a static array.#include <stdio.h>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 allocationExplicit allocation and de-allocation by user using malloc() and free().(void *)malloc(size_t size);void free(void *ptr);bytes sizeof(type)int *ptr; ?ptr4000ptr = (int*)malloc(4 * sizeof(int)); //Address 6000 on the heap is allocated60006000600460046008600860126012 ????????6000*ptr=4;4free(ptr);Man pages – Section 3>>man –s 3 free>>man –s 3 mallocMemory allocation for a process.Code segmentStatic data segmentDynamic data segment(heap)Stackmalloc looks for space on the heapint *p; //p is created in the static data segmentp = (int *)malloc(4 * sizeof(int)); //Space for 4 ints i.e. contiguous 16 //bytes is allocated on the heapDynamic arrayint *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)Array of PointersVariable length stringschar *card[4]; // card[4] => array of 4 elements //char* => element is a pointer to a character. // card[4] => array of 4 char pointers4000 card[0]4004 card[1]4008 card[2]4012 card[3]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 space15Common errors - Memory leakint *ptr, x; ptr = (int*)malloc(10*sizeof(int)); //ptr gets space //starting at address 3000ptr = &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.16Common erros - Dangling pointersint *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 =
View Full Document