#Could realloc change the memory address even if shrunk?

5 messages · Page 1 of 1 (latest)

grizzled magnetBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

vapid panther
#

// does temp always equal arr?
no. realloc is allowed to return NULL.

Could realloc change the memory address even if shrunk?
that an impl detail and is very much depends on your allocator. i'd assume sane allocators won't allocate a new block and copy the content over though nothing say they couldn't

half pike
#

"The reallocation is done by either:
a) expanding or contracting the existing area pointed to by ptr, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. If the area is expanded, the contents of the new part of the array are undefined.
b) allocating a new memory block of size new_size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block."

I assume it would always be possible to contract an area pointer to, so in that case, it would remain the same pointer. but maybe there are some weird edge cases I don't know about

grizzled magnetBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity