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?

H_squared
  • 1,251
  • 2
  • 15
  • 32
  • 1
    OT: I feel `(iCount + 1) * sizeof(MAP)` at least looks nicer ... – alk Sep 04 '13 at 15:34
  • @alk: you're right. Will change it. – H_squared Sep 04 '13 at 15:35
  • 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. – Keith Thompson Sep 04 '13 at 15:40
  • 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.) – rici Sep 04 '13 at 16:42
  • @rici 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. – H_squared Sep 05 '13 at 07:46

3 Answers3

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 4
    `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. – Keith Thompson Sep 04 '13 at 15:37
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.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
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>.)

Paul R
  • 208,748
  • 37
  • 389
  • 560
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 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. – H_squared Sep 05 '13 at 07:50
  • @hhachem: then skip the call in case the allocation size doesn't change. – Fred Foo Sep 05 '13 at 08:38