Realloc

P

polas

Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory. However, when I call
realloc on an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.

Therefore I was wondering if the realloc will only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?

Cheers,
Nick
 
C

Chris Dollin

polas said:
Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory.
However, when I call
realloc on an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.

What makes you think so? (Other than that the implementation may
impose a limit on the size of mallocated objects, or have limited
space available.)
Therefore I was wondering if the realloc will only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?

Not and be standard-conformant. (And assuming you haven't trashed
the space in which malloc/realloc/free operate.)

Note that `realloc` is free to allocate fresh memory if it can't
expand in-place.
 
W

Walter Roberson

Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory. However, when I call
realloc on an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.

No, there is no more limit than there is for the malloc() case.

Therefore I was wondering if the realloc will only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, could realloc ever
overwrite other, existing data?

realloc() will never (in any implementation) allocate memory that
overlaps with any other current allocation (not unless you've accidently
bashed it's control structures). But if you have allocated
memory and freed it, and then continue to use the memory in violation
of the implied contract between you and the allocator, then Sure, that
no-longer-yours memory could get overwritten; the same can happen
if you write outside of an allocated object, since outside of the object
might be memory that the allocator was using to keep track of what
was allocated.
 
I

inder.kumar

Hi all - just a question Im wondering about. I realise that this
*might* be OS specific, however in general when I call a malloc then I
can allocate an arbitrary amount of memory. However, when I callreallocon an already existing block of memory (to increase it's size)
then there is a limit to the size it will go.

realloc() returns a pointer to the newly allocated memory... The
current block may be moved in case more memory is required to get
contiguous block... The realloc() takes care of free() etc if it is
required in case memory block has been copied to another block...
Therefore I was wondering if thereallocwill only increase the size
of an allocated memory block whilst the continous memory its
increasing into is free. Depending on this, couldreallocever
overwrite other, existing data?

Cheers,
Nick

- Surinder Kumar
http:///www.techpenguin.com/
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top