Does a realloc to smaller memory result in the same pointer

Hi all

Just a quick question ( I could probably test this out for myself...but I'm lazy... ).

If I do this:
void* testPtr = malloc ( 1000 )

And then this:
testPtr = realloc ( testPtr, 500 )

Will the realloc just reduce the allocated size and keep the same pointer, or can there be a chance of it finding another place for that allocation ( Meaning that it will expensively move the memory to another location )?

I am trying to create efficient programs by making my dynamic allocations the least resource hungry as possible during runtime.

Many Thanks,
SuperStinger
Last edited on
> Will the realloc just reduce the allocated size and keep the same pointer

This is all that is guaranteed:
The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. If there is not enough memory, the old memory block is not freed and null-pointer is returned.
...
If new_size is zero, the behavior is implementation defined

Implementations with low memory fragmentation as a goal (for instance, ones in smart phones) may move the block to another location that provides a better fit.
Cheers, I guess I'll trial the function my self and examine its behavior. Thanks for the reply! Really Helpful
Topic archived. No new replies allowed.