1

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?

Divya
  • 393
  • 2
  • 5
  • 17
  • Subtle differences that likely make no _functional_ difference for OP: 1) If more memory is allocated, that additional is not known to be initialized to 0 - initial values are undefined. The memory originally allocated with `calloc()` is zero'd before first access, though not necessarily at `calloc()` time. 2) `calloc()` can allocate more memory than `malloc()/realloc()` as the `calloc()` call is limited to theoretical `SIZE_MAX*SIZE_MAX`. This occurred in earlier platforms - do not see it now. – chux - Reinstate Monica Aug 08 '15 at 05:24
  • 1
    Why are there so many posts starting out "We know that " – M.M Aug 08 '15 at 05:27
  • @MattMcNab Indeed, but OTOH let's be thankful that the underlying fallacy or false premiss is so often right there at the start :-| – user207421 Aug 08 '15 at 06:19
  • @DivyaBolla As you've edited out the first sentence, you've now removed the only thing that motivated the question at all. If you now understand that the memory layout isn't different, what's the point of the question? – user207421 Aug 08 '15 at 07:33

3 Answers3

10

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 calloced blocks in exactly the same way it handles malloc()ed blocks.

user207421
  • 305,947
  • 44
  • 307
  • 483
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • When we request single contiguous memory block using malloc, we request several block of contiguous memory using calloc. How do we request new blocks of memory using realloc? I'm a bit confused about the realloc function parameters if the memory we are trying to change is allocated using calloc – Divya Aug 08 '15 at 05:06
  • 2
    With `calloc()`, you get a single block of memory, just like you do with `malloc()` and just like you do with `realloc()`. – Jonathan Leffler Aug 08 '15 at 05:11
  • @Divya Bolla: The language has no concept of "several contiguous blocks of memory". The only way to produce something like that is to allocate a single block of combined size. So, `calloc` allocates a single block of memory. It just takes care of multiplying the parameters `nmemb` and `size` (per C11 naming) in order to calculate the size of that single block. There's also a subtle matter of making sure that multiplication does not overflow (if I remember correctly `calloc` is responsible for making sure of that too). – AnT stands with Russia Aug 08 '15 at 07:27
2

memory block layout is different for malloc and calloc

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.

EDIT

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.

user16217248
  • 3,119
  • 19
  • 19
  • 37
ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • char *ptr = calloc(5,sizeof(char)); now i need 7 blocks each of size char.. ptr=(char *)realloc(ptr, <>); What parameters should we give in the <> ? – Divya Aug 08 '15 at 05:09
  • @DivyaBolla: you could use `7 * sizeof(char)`, but I'd just write `7`. And you don't need to cast the void* returned by realloc any more than you need to cast the void* returned by calloc. – rici Aug 08 '15 at 05:18
  • @DivyBolla `7*sizeof char`, of course. What's the problem? And it isn't '7 blocks each of size char', it is *one* block of size 7 chars. – user207421 Aug 08 '15 at 05:18
  • @DivyaBolla I think you are overthinking this. Please see the edit. – ameyCU Aug 08 '15 at 05:30
2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 5
    "calloc(n, size) is nothing more than malloc(n*size) followed by memset()." - that's true in the abstract machine. Some actual implementations implement calloc in a different way, which could be what confused OP. – M.M Aug 08 '15 at 05:29
  • @MattMcNab Hard to see how. He wouldn't be able to see how it's implemented, and if he could he wouldn't need to ask the question. I'm also puzzled how he can think 7 adjacent blocks of memory one char wide can possibly have any internal formatting, but that's another issue. – user207421 Aug 08 '15 at 06:08