0

I'm struggling with realloc...

strucType mkBggr (structType x, char ch) { 
    x = realloc(x, 100);
    printf("%d", sizeof(x));
}

I'm thinking this should print out the value 100, but it prints 8.

It obviously has something to do with pointers, but I've no idea what. I've added *s and &s in front of the x's, but I don't seem to get it. Any help is appreciated!

  • 1
    Add definition of `structType`, please. – hyde Aug 29 '14 at 19:05
  • The question is nothing to do with `realloc`. Remove the call to `realloc` and the output remains the same. Ask yourself what `x` is and what `sizeof` means. – David Heffernan Aug 29 '14 at 19:09
  • Btw, correct way to use *realloc* for this code would be: `structType tmp = realloc(x, 100); if (tmp) { x = tmp; } else { /*...realloc failed, need to keep old value of x...*/ }` – hyde Aug 29 '14 at 19:11
  • This has been asked several times on SO, and I remember myself having answered several times also. – Basile Starynkevitch Aug 29 '14 at 19:18
  • @hyde A failure always return `NULL, but `realloc()/malloc()` returning `NULL` does not always indicate failure. Consider when [`size == 0`](http://codereview.stackexchange.com/questions/36662/critique-of-realloc-wrapper) – chux - Reinstate Monica Aug 29 '14 at 20:04
  • @chux True, something to keep in mind, though the function name `mkBggr` here implies that should not be the case in the real code, and with literal `100` it won't definitely be. – hyde Aug 30 '14 at 06:32

2 Answers2

2

Realloc returns a pointer, so x is of pointer type. sizeof(x) is returning the size of a pointer, which is 8 bytes in this environment.

David S.
  • 518
  • 3
  • 5
0

It prints the value 8 because you're asking for the size of the pointer (x). You cannot print the size of a block of allocated memory - you'll have to track that yourself.

This is why I don't recommend using realloc; allmost everyone I see asking about it is using it wrong.

Frank H.
  • 1
  • 1
  • 11
  • 25
  • 1
    I'd like to reallocate a block of memory to change the size but retain the content the endures. Please recommend a way to do this if not `realloc`. – David Heffernan Aug 29 '14 at 19:07
  • 1
    @DavidHerrnan: Just use `malloc`, `memcpy`, and  `free` explicitly, testing `malloc` against failure before `free`. This is much better than a `realloc` – Basile Starynkevitch Aug 29 '14 at 19:19
  • @Basile Starynkevitch please consider posting a `malloc, memcpy, free` solution on maybe codereview.stackexchange.com or SO. – chux - Reinstate Monica Aug 29 '14 at 20:01
  • @BasileStarynkevitch Why is it better to use those explicitly? – hyde Aug 30 '14 at 05:42
  • Because doing things explicitly handle much better the failure cases. A naive `ptr = realloc(ptr,size);` is losing `ptr` on failure.... – Basile Starynkevitch Aug 30 '14 at 05:45
  • @BasileStarynkevitch But that is using `realloc` wrong, simple as that... Not a reason to avoid using it right, when alternative is replacing one function call with 3 calls. – hyde Aug 30 '14 at 05:54