views:

247

answers:

5

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?

+3  A: 

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;

Computer Guru
+2  A: 

Yes it does

bmargulies
sorry about the shouting but it helped get the short answer past the minumum length requirement.
bmargulies
+5  A: 

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.

Mitch Wheat
+4  A: 

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.

Hope this helps, Best regards, Tom.

tommieb75
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?
zajcev
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...
tommieb75
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?
Secure
@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! :)
tommieb75
@Secure: the old malloc returned void * hence the cast, I merely forgot to put that in for the realloc..
tommieb75
@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*...
Secure
@Secure: not pre-ansi, Ansi C 89 standard...as for your comment - good point!!! Will edit this accordingly...
tommieb75
@Secure: Edited the answer! Hope it makes better sense! :) Thanks for your feedback. :)
tommieb75
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.
zajcev
@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...
tommieb75
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 set
zajcev
@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... :)
tommieb75
A: 

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.

R..