Behavior of realloc when the new size is the same as the old one
Asked Answered
A

3

10

I'm trying to make a code more efficient. I have something like this:

    typedef struct{
    ...
    }MAP;



    MAP* pPtr=NULL;
    MAP* pTemp=NULL;
    int iCount=0;
    while (!boolean){
    pTemp=(MAP*)realloc(pPtr,(iCount+1)*sizeof(MAP));
    if (pTemp==NULL){
    ...
    }
    pPtr=pTemp;
    ...
    iCount++;
    }

The memory is being allocated dynamically. I would like to reduce the realloc calls to make the code more efficient. I would like to know how realloc would behave if the new size is equal to the old one. Will the call be simply ignored?

Agoraphobia answered 4/9, 2013 at 15:15 Comment(5)
OT: I feel (iCount + 1) * sizeof(MAP) at least looks nicer ...Cyrillic
@alk: you're right. Will change it.Agoraphobia
Most implementations probably return the same pointer, but some might sometimes use a realloc with the same size as an opportunity to relocate the allocated space to make future allocations easier.Ademption
Why do you care? Your code has to work regardless of whether you get a new pointer or not from realloc, so it should not make any difference to your code whether or not you call realloc when it is not "necessary". In any event, I doubt that you'll save much execution time by avoiding the realloc call and you may have already wasted too much of your time :) Beware of premature optimization (but ignore me if you've profiled or some such.)Beckett
@Beckett My boss would like me to program more efficiently. He suggested allocating more memory (like 20*sizeof(MAP)) than needed each time, then realloc each time 20 MAPs have been saved. It is just an experiment. He himself hasn't tried it yet, but would also like to know.Agoraphobia
B
11

It's not specified in standard C. All standard C guaranteed is: the contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes.

However, if you are using GNU libc, it says explicitely to return the same address, see here for detail.

If the new size you specify is the same as the old size, realloc is guaranteed to change nothing and return the same address that you gave.

Burdick answered 4/9, 2013 at 15:26 Comment(1)
realloc is provided by GNU libc, not by gcc, which is just the compiler. On many systems, gcc is used with libraries other than GNU libc.Ademption
J
3

I wouldn't count on realloc behaving as a no-op if the size doesn't change (although it seems a reasonable thing for it to do), but then you don't have to: if the value of iCount hasn't changed, don't make the call to realloc.

Joanne answered 4/9, 2013 at 15:18 Comment(0)
R
2

The C standard doesn't specify what will happen, so you're at the mercy of the implementation. I can't imagine any half-decent implementation that will not return the pointer passed in, but the safe option is to check whether the size of the allocation has changed in your own code. That will also certainly skip a function call.

(BTW, please don't cast the return value from realloc. It's not necessary and it may hide undefined behavior if you forget to #include <stdlib.h>.)

Rodl answered 4/9, 2013 at 15:19 Comment(2)
I think checking if realloc returns NULL is critical, since if it fails, I end up with a segmentation fault. As far as I know, every single realloc call available online checks if the output is NULL.Agoraphobia
@hhachem: then skip the call in case the allocation size doesn't change.Rodl

© 2022 - 2024 — McMap. All rights reserved.