how the realloc( ) allocates memory ?

M

M a n i s h

is there any difference in how the realloc() reallocates memory to
varibles which were being allocated memory using malloc() and calloc ?
 
J

Joona I Palaste

M a n i s h said:
is there any difference in how the realloc() reallocates memory to
varibles which were being allocated memory using malloc() and calloc ?

This would have to be an implementation-specific detail.
 
F

Floyd Davidson

Joona I Palaste said:
This would have to be an implementation-specific detail.

Is that really true, in respect to what the OP is asking?

The realloc, calloc and malloc functions all perform "magic"
behind the scenes, and the details are not available to the
programmer. But outwardly, "magic" is spelled the same for all
three.

Both the realloc and free functions can operate on a pointer
returned by any of the three. I think that is what the OP
probably meant by "is there any difference", and for the
programmer the answer is no, they are the same.

Of course, underneath they might accomplish "magic" in three
distinct ways... but that is of concern only to the implementor.
 
R

Richard Bos

is there any difference in how the realloc() reallocates memory to
varibles which were being allocated memory using malloc() and calloc ?

Internally, no idea. But it doesn't matter, since the effect is
identical - you are either given a pointer to the new memory, or a null
pointer.

Richard
 
N

Nils Petter Vaskinn

Is that really true, in respect to what the OP is asking?

The realloc, calloc and malloc functions all perform "magic"
behind the scenes, and the details are not available to the
programmer. But outwardly, "magic" is spelled the same for all
three.

Both the realloc and free functions can operate on a pointer
returned by any of the three. I think that is what the OP
probably meant by "is there any difference", and for the
programmer the answer is no, they are the same.

Of course, underneath they might accomplish "magic" in three
distinct ways... but that is of concern only to the implementor.

I think what the OP was interested in may have been:

Q: Does realloc treat a pointer to calloced memory different from malloced
memory, will the "new part" of a calloced array that grows be zeroed?

A: An implementation could do this, but the standard doesn't say it
should, so for portable code you can't depend on it.

OP:
You could write your own clearing realloc (crealloc?) like this:

crealloc (void *,size_t new_size, size_t current_size)

That calls realloc behind the scene and then memsets the new portion of
the array.
 
V

Vijay Kumar R Zanvar

Is that really true, in respect to what the OP is asking?

I suppose OP stands for Original Poster.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top