The information for the size of the memory allocated to a pointer, is stored at the location 4 bytes ahead of the address of the pointer.(32 bit sys) if the address of the pointer is 0x8920208 then the memory size allocated to pointer is stored at 0x8920204
see the following example:
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 int i=0; 6 7 int *ptr; 8 char *str; 9 ptr = malloc(sizeof(int)); 10 printf("the size of memory allocated (of int type)is %d\n",*(ptr-1 )); 11 12 str = malloc(sizeof(char)); 13 printf("the size of memory allocated (of char type)is %d\n\n",*(str-4)); 14 15 while(i<100) 16 { 17 i++; 18 ptr = malloc(sizeof(int)*i); 19 printf("the size of memory allocated (of int type) for %d is== %d\n",i,*(ptr-1)); 20 } 21 i=0; 22 23 while(i<100) 24 { 25 i++; 26 str = malloc(sizeof(char)*i); 27 printf("the size of memory allocated (of char type) for %d is== %d\n",i,*(str-4)); 28 } 29 return 0; 30 }