Reputation: 393
When we try to resize the memory allocated by malloc
using realloc
, we typically do this:
char *ptr = (char *)malloc(size_1);
ptr = (char *)realloc(ptr, size_2);
If size_2
may be larger or smaller than size_1
. If new size is larger then the old data is not lost and newly allocated bytes are uninitialized. The starting address contained by the ptr
may change if there is not sufficient memory at the old address to store all bytes consecutively. realloc
moves the contents of old block into the new block and ptr
will be pointing to the initial byte of this new block.
But, if memory is allocated using calloc
, I was not able to understand how realloc
function acts. Can someone please give me a brief overview about how realloc
works on memory allocated by calloc
?
Upvotes: 1
Views: 14800
Reputation: 16607
memory block layout is different for
malloc
andcalloc
Actually no. The difference between calloc
and malloc
is calloc()
initializes the allocated memory with 0 values, whereas malloc()
doesn't initialize the allocated memory, so the memory will have undefined/garbage data.
And the number of arguments into them.
And I don't think that realloc
treats memory allocated by malloc
or calloc
differently.
On your problem:
a = calloc(5,sizeof(int));
if (a == NULL)
{
printf("Error in allocating memory");
}
a = realloc(a,7); // Also check its return.
This will work as intended.
Upvotes: 3
Reputation: 310911
We know that the memory block layout is different for malloc and calloc.
No. It is the same. There is no basis for this assertion.
When we try to resize the memory allocated by malloc using realloc, we typically do this:
char *ptr=(char *)malloc(size_1);
ptr=(char *)realloc(ptr, size_2);
[where] size_2 may be larger or smaller than size_1. If new size is larger then the old data is not lost and newly allocated bytes are uninitialized. The starting address contained by the ptr may change if there is not sufficient memory at the old address to store all bytes consecutively.
Correct.
realloc moves the contents of old block into the new block
If necessary. If the block was merely grown, this step isn't necessary.
and ptr will be pointing to the initial byte of this new block.
Correct.
But, if memory is allocated using calloc, i was not able to understand how realloc function acts.
The same.
Can someone please give me a brief overview abt realloc works on memory allocated by calloc?
It is the same.
You're overthinking this. calloc(n, size)
is implementable with nothing more than malloc(n*size)
followed by memset()
. realloc()
doesn't care.
Upvotes: 2
Reputation: 320491
We know that the memory block layout is different for malloc and calloc.
No, we don't. Actually, we know that there's no difference at all, aside from the fact that calloc()
is responsible for multiplying its parameters (to determine the block size) and making sure that the allocated block is initialized with all-zero bit pattern.
The rest follows. There's no difference in how the memory block is treated by realloc()
, regardless of what function was used to allocate it. realloc()
handles calloc
ed blocks in exactly the same way it handles malloc()
ed blocks.
Upvotes: 12