Possible Duplicate:
Malloc and Realloc relation, How does it handle when the required space is not available in memory
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *p;
p = malloc(6);
p = realloc(p, 10);
if (p == NULL)
{
printf("error"); // when does p point to null consider i have enough space in prgrm
//memory area but not in memory where realloc is trying to search
//for the memory, I dont know how to explain that try to undrstnd
exit(1);
}
}
Take this example for the code, suppose the total memory is 10 bytes and 2 bytes is used by the declaration of pointer to type int and ohter 6 bytes by malloc function the remaining 2 bytes is occupied by other programs, now when i run realloc function to extend the memory that pointer is pointing to, it will search for 10 bytes in memory and when it is not available it allocates 10 bytes of memory from heap area and copies contents of malloc and paste it in new allocated memory area in heap area and then delete the memory stored in malloc right?
Does realloc() return a NULL pointer because the memory is not available? No right!? It does go to heap area for the memory allocation right? It does not return a NULL pointer right?
Listen to me: | 01 | 02 | 03 | 04 | 05 | 06 | 07 |08 |09 | 10 |
consider this as memory blocks: assume that 01 to 06 has been used by malloc() func, 07 and 08 are free and last 2 blocks i,e 09 and 10 are being used by memory of other programs. Now when i call realloc(p,10) i need 10 bytes but there are only 2 free bytes, so what does realloc do? return a NULL pointer or allocate memory form the heap area and copy the contents of 01 to 06 blocks of memory to that memory in the heap area, please let me know.