How to update other pointers when realloc moves the memory block?
Asked Answered
A

5

20

The realloc reference says:

The function may move the memory block to a new location, in which case the new location is returned.

Does it mean that if I do this:

void foo() {

        void* ptr = malloc( 1024 );

        unsigned char* cptr = ( unsigned char* )ptr+256;

        ptr = realloc( ptr, 4096 );
}

then cptr may become invalid if realloc moves the block?

If yes, then does realloc signal in any way, that it will move the block, so that I can do something to prevent cptr from becoming invalid?

Amaty answered 31/1, 2010 at 15:4 Comment(1)
This is a good question +1 from me as it emphasize the fundamental learning curve involving realloc...Levitan
L
9

Yes, cptr will become invalid as realloc moves the block! And no, there is no mention of signalling to you to tell that it is moving the block of memory. By the way, your code looks iffy...read on... please see my answer to another question and read the code very carefully on how it uses realloc. The general consensus is if you do this:

void *ptr = malloc(1024);

/* later on in the code */

ptr = realloc(ptr, 4096);

/* BAM! if realloc failed, your precious memory is stuffed! */

The way to get around that is to use a temporary pointer and use that as shown:

void *ptr = malloc(1024);

/* later on in the code */

void *tmp = realloc(ptr, 4096);

if (tmp != null) ptr = tmp;

Edit: Thanks Secure for pointing out a gremlin that crept in when I was typing this earlier on.

Levitan answered 31/1, 2010 at 15:15 Comment(13)
It's not real code - it's just something to show what I was going to do. So is there any way to resize an existing block of memory without moving it ( or tell the program that it's not possible? ). If I call realloc and it succeeds by moving the block, then is it possible to somehow keep the old addresses valid, so that I can "undo" the realloc?Amaty
No! You cannot do that...that is compiler/linker/runtime dependent. And no you cannot keep the old addresses as the heap will get fragmented over the course and lifetime of the program. In short, you cannot absolutely pin down the old addresses as the very essence of pointers is based on dynamic addressing...and no you cannot undo the realloc...Levitan
This is the most useless cast of the return value of malloc I've ever seen. And why do you cast the return of malloc, but not the return of realloc?Sabra
@Secure: sorry about that - that's old skool C programming....and firmly engrained into my brain...ever worked with C 89 standard in early 90's, then you would know what I'm talking about! :)Levitan
@Secure: the old malloc returned void * hence the cast, I merely forgot to put that in for the realloc..Levitan
@tommieb75, I assume you mean pre-ANSI compilers that used char* for the return of malloc. Else your second comment doesn't make sense, casting a void* to a void*...Sabra
@Secure: not pre-ansi, Ansi C 89 standard...as for your comment - good point!!! Will edit this accordingly...Levitan
@Secure: Edited the answer! Hope it makes better sense! :) Thanks for your feedback. :)Levitan
I found a way to get this done - its linux specific, but it's all I need. There's mmap and mremap functions on linux that one can use to allocate memory ( see flag MAP_ANONYMUS ), and then resize it assuring that it will not be moved.Amaty
@zajcev: I am not sure I follow you, mmap is more for IPC rather then anything else, it does not do the same thing as what the C runtime realloc does, mmap is a POSIX function, not a ANSI C runtime function...Levitan
mmap can be used to allocate memory too, and mremap will try to reallocate memory in place and only move the block if a flag is setAmaty
@zajcev: I'm curious, can I see the source for that information as that is a new one to me? Thanks for that interesting tidbit... :)Levitan
If the new pointer differs from the old pointer, the memory has been moved. So there is a signaling.Whiny
S
6

This is coming a bit late, but the solution to this problem (which nobody has mentioned) is not to use pointers into allocated blocks that will need to be allocated. Instead, use integer-valued offsets from the base pointer or (better) use a struct type and member elements to address specific locations in the allocated object.

Stendhal answered 5/10, 2010 at 4:6 Comment(1)
Wish I had through of that before I wrote my current assignment. I also think this is part of the answer to zajcev's OP, explaining what he can do about it.Gauntlett
G
4

Yes, cptr will become invalid if realloc moves the block.

No, there is no signal. You would have to check the return value against the original ptr location.

Ginkgo answered 31/1, 2010 at 15:7 Comment(0)
T
3

Yes.

Best thing to do is compare ptr before and after the reallocation, and see if it has been moved. You shouldn't assign a pointer to the offset value, instead you should store the offset and then index the original operator with it.

i.e.

Instead of void* newPtr = ptr + 10; *newPtr = something;

Use int new = 10; ptr[new] = something;

Teutonize answered 31/1, 2010 at 15:6 Comment(0)
E
2

Yes, the cptr becomes invalid if realloc moves the block.

Extrorse answered 31/1, 2010 at 15:7 Comment(1)
sorry about the shouting but it helped get the short answer past the minumum length requirement.Extrorse

© 2022 - 2024 — McMap. All rights reserved.