Pointer Dynamic Memory Allocation
Pointer Dynamic Memory Allocation
Constant pointer Constant pointer means the pointer is constant. Ex. int a=10,b=20; int * const ptr=&a; ptr=&b; *ptr=15; int * const ptr indicates that ptr is a pointer which is constant. ptr cannot be made to point to another integer. Pointer to constant Pointer to constant means the pointer points to a constant. Ex. int a=10,b=20; const int * ptr; ptr=&a; ptr=&b; *ptr=15; const int * ptr indicates that ptr is a pointer that points to a constant integer The integer is constant and cannot be changed. Constant pointer to constant Constant pointer to constant means pointer is constant and points to a constant. Ex. int a=10,b=20; const int * const ptr=&a; ptr=&b; *ptr=15; Void pointer Special type of pointer that can be pointed at objects of any data type. A void pointer is declared like a normal pointer, using the void keyword as the pointers type: void *ptr; //ptr is a void pointer Void pointer can point any type of variable. When we assign content of void pointer to other must use type casting. int a=10,*ptr; char ch=A ,*ctr; float fl=1.5 ,*ftr; void * vtr; vtr=&a; vtr=&ch; vtr=&fl; ctr=(int *)vtr
#include <stdlib.h> #include <stdio.h> void main() { /* allocate memory for a 50-integer */ int *ptr; ptr=(int *)malloc(100); if(ptr==NULL) { printf( "Not enough memory to allocate buffer\n"); exit(1); } printf( "Memory was allocated!"\n ); }
#include <stdio.h> void main() { int *ptr,n,i; printf(Enter No. of element \n); scanf(%d,&n); ptr=(int *)malloc(n*sizeof(int)); if(ptr==NULL) { printf( "Not enough memory to allocate buffer\n"); exit(1); } printf( "Memory was allocated!"\n ); printf(Enter elements \n); for(i=0;i<=n-1;i++) scanf(%d,(ptr+i)); printf(Elements of array is\n); for(i=0;i<=n-1;i++) printf(%d,*(ptr+i)); }
calloc() Function The calloc() function also allocates memory. calloc() allocates a group of objects. Prototype of function void *calloc(num,size); num is the number of objects to allocate. size is the size (in bytes) of each object. If allocation is successful, all the allocated memory is cleared (set to 0).
#include <stdlib.h> #include <stdio.h> void main() { int *ptr,n,i; printf(Enter No. of element \n); scanf(%d,&n); ptr=(int *)calloc(n,sizeof(int)); if(ptr==NULL) { printf( "Not enough memory to allocate buffer\n"); exit(1); } printf( "Memory was allocated!"\n ); printf(Enter elements \n); for(i=0;i<=n-1;i++) scanf(%d,(ptr+i)); printf(Elements of array is\n); for(i=0;i<=n-1;i++) printf(%d,*(ptr+i)); }
Difference Between Malloc and Calloc There are two differences 1) Number of arguments malloc() takes a single argument (memory required in bytes). calloc() needs two arguments. 2) Default value malloc() does not initialize the memory allocated. calloc() initializes the allocated memory to ZERO.
realloc() Function The realloc() function changes the size of a block of memory that was previously allocated with malloc() or calloc(). The function prototype is void *realloc(void *ptr, new_ size); The ptr argument is a pointer to the original block of memory The new size, in bytes, is specified by new_ size free() Function When you allocate memory with either malloc() or calloc(), it is taken from the dynamic memory pool that is available to your program. This pool is sometimes called the heap, and it is finite. The function prototype is void free(void *ptr);