3

I saw some code related to realloc() on some sites as below.

int *p = (int *)malloc(sizeof(int) * 10);
p = (int *)realloc(p, 100);

But as the standard says, if realloc fails, the original block is left untouched and it returns NULL.

So if realloc fails, from above example, we will lose the ability to free p. Can any one please let me know is it good coding practice to assign the address returned by realloc() to the same pointer?

kadina
  • 5,042
  • 4
  • 42
  • 83
  • 2
    Unrealed, so long as you're on the subject of "good coding practice", maybe read this: [Do I cast the result of `malloc`?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – WhozCraig Jul 05 '16 at 21:35
  • *Can any one please let me know is it good coding practice to assign the address returned by realloc() to the same pointer?* Right after *So if realloc fails, from above example, we will lose the ability to free p.*? What do you think? – Andrew Henle Jul 05 '16 at 21:35
  • 2
    Whoever voted to close, how is this "primarily opinion based"? The practice OP asked about is **always a bug**. – R.. GitHub STOP HELPING ICE Jul 05 '16 at 21:36
  • @AndrewHenle : I think it is not right to assign directly. But I want to confirm that. – kadina Jul 05 '16 at 21:38
  • @WhozCraig : Thanks for the link. – kadina Jul 05 '16 at 21:39
  • @evaitl: While the answer covers some of the same material, that's a very different question. In that question, the OP is asking why `realloc` can't modify the caller's pointer variable itself. – R.. GitHub STOP HELPING ICE Jul 05 '16 at 21:42
  • @R..: I don't *quite* agree that it's "always a bug". If the response to a `realloc` failure is to terminate the program immediately, there's no harm in clobbering the previous value of `p`. (I'm assuming `malloc`ed memory is automatically `free`d when the program terminates -- a reasonable assumption for any hosted implementation, even though it's not explicitly guaranteed by the standard.) I see you addressed this in your answer. – Keith Thompson Jul 05 '16 at 22:01
  • @KeithThompson: Yep. My answers tend to be less absolutist and more detailed/forgiving, probably because they're subject to voting. :-) – R.. GitHub STOP HELPING ICE Jul 05 '16 at 22:19

2 Answers2

7

You are correct that directly assigning the return value of realloc to your only copy of the original pointer is a bad practice. Not only do you lose the ability to free the memory if realloc failed (I would call this the lesser issue); you also lose the data that your pointer pointed to.

For some programs this may not matter (e.g. if you're just going to terminate and abort the entire operation on allocation failures, which may or may not be acceptable practice, and which is a whole topic in itself) but in general you need to first store the result of realloc to a separate temp variable, and only overwrite the original pointer variable after you check that it succeeded.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
3

R. gave a clear answer to your question. Let me emphasize two other issues in the code fragment:

int *p = (int *)malloc(sizeof(int) * 10);
p = (int *)realloc(p, 100);

It is mostly considered bad practice in C to cast the return value of malloc(), calloc() and realloc(). The void * return value will automatically be converted to the appropriate pointer type, unless the code is compiled as C++ code, which is an estranged cousin of C.

More importantly, it does not make sense to realloc the pointer to a hardcoded size 100. The number of ints that will be accessible in the reallocated array will depend on the actual size of the int type, which can vary from one system to another. Indeed on some architectures, 100 is not even a multiple of sizeof(int). The author might have meant to write p = realloc(p, sizeof(int) * 100); or p = realloc(p, sizeof(*p) * 100);, and more context around the fragment could help understand the intent... As written, it is very likely a bug, for multiple reasons.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thanks. I just copied one of the examples shown on the internet sites. But I understood the point. – kadina Jul 06 '16 at 15:07