Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.

Similar presentations


Presentation on theme: "Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training."— Presentation transcript:

1 Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training

2 Training C/C++ EcoSoftware 2 ADVANCED USER OF POINTER o Dynamic storage of allocation. o Dynamically allocated string. o Dynamically allocated array. o Deallowcating storage.

3 Dynamic storage of allocation o The ability to allocate storage during program execution o Using dynamic storage allocation, we can design data structure as needed. o Memory allocation functions. Training C/C++ EcoSoftware 3

4 Dynamic storage of allocation o Null pointer :  When the memory allocation is call that posibility it won’t be able to locate a block of memory large enough to satify our request.  Function will be result null pointer.  Example. Training C/C++ EcoSoftware 4

5 Dynamically allocated String o Using malloc.  Syntax :  If you want to allocate space for a string of n charaters. (note: sizeofchar(char) = 1); char *p = (char*)malloc(n+1);  Memory allocated using malloc isn’t clear.  Using strcpy is one way to init value. strcpy(p,abc) Training C/C++ EcoSoftware 5

6 Example Dynamically allocated String Training C/C++ EcoSoftware 6

7 Dynamically allocated Array o Using malloc can allocate space for an array. o The calloc fucntion sometime use instead. o The realloc function allow us to make an array “grow” or “shrink”. o Using malloc to allocate for an array.  Syntax : datatype *pointer = (datatype *)malloc(n*sizeof(datatype ));  Example : int *a = (int*)malloc(n*sizeof(int)) ; Training C/C++ EcoSoftware 7

8 Using calloc to allocate for an array. o It sometime better than malloc.  Syntax :  Example : int *a = (int *)calloc(n,sizeof(int)); Training C/C++ EcoSoftware 8

9 Using realloc to allocate for an array. o We have allocate memory for an array. It too large or too small. o The realloc function can resize the array to better. o allocate function in. o Syntax :  p must point to memory block obtained by a provious call of malloc or calloc or realloc.  size paramater represent the new size of the block Training C/C++ EcoSoftware 9

10 Using realloc to allocate for an array. o Role of realloc fucntion. Training C/C++ EcoSoftware 10

11 Deallocating storage. o Using free function for deallocate. o Syntax :  void free(void *p); o Example :  char *p = (char*)malloc(4);  free(p); Training C/C++ EcoSoftware 11

12 Training C/C++ EcoSoftware 12 Thank You End


Download ppt "Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training."

Similar presentations


Ads by Google