2

I have this problem in a bigger project, in which for some reason the realloc function does absolutely nothing. Am I missing something obvious?

Here is a simplified example:

#include <stdio.h>
#include <string.h>

int main()
{
    int x = 1, y = 2, z = 3;
    int* arr, *arr1;
    int** arra;

    arra = (int**)malloc(sizeof(int*));
    arr = (int*)malloc(sizeof(int));
    arr[0] = x;
    arra[0] = arr;
    arra = (int*)realloc(arra, sizeof(int*) + sizeof(int*));
    arr1 = (int*)malloc(sizeof(int));
    arr1[0] = y;
    arra[1] = arr1;

}

When I debugged, the final arra was {{1}}, even though to my understanding it should be {{1},{2}}.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • 4
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Jan 04 '16 at 19:20
  • you need `#include `. and `(int*)realloc` --> `(int**)realloc` – BLUEPIXY Jan 04 '16 at 19:22
  • 1
    @BLUEPIXY That is precisely one reason not to cast `malloc()`. – Iharob Al Asimi Jan 04 '16 at 19:23
  • 2
    Also, don't `realloc()` directly like that, in certain situations the code will cause a memory leak and/or data loss. – Iharob Al Asimi Jan 04 '16 at 19:24
  • I don't think the debugger knows ho much you allocated. Try replacing `arr=malloc(sizeof(int)); arr[0]=x;` with `arr=malloc(2*sizeof(int)); arr[0]=x; arr[1]=z;` and see if the debug output changes. – Mad Physicist Jan 04 '16 at 19:26
  • Did you consider using [flexible array members](https://en.wikipedia.org/wiki/Flexible_array_member)? They are probably very relevant for your case. – Basile Starynkevitch Jan 04 '16 at 19:27
  • Flexible array members have nothing to do with the issue at hand. – Mad Physicist Jan 04 '16 at 19:28
  • @MadPhysicist: flexible array members would be useful here. They strongly suggest to keep the size of the flexible array in the same `struct`, and that is good design – Basile Starynkevitch Jan 04 '16 at 19:29
  • @BasileStarynkevitch, using arrays of one cell and reallocating them to two cells is not worth to think even in using dynamic memory, as they can be allocated as local variables, so being useful or not is not an issue. I think the asker wants to know is why realloc has done nothing and probably it is due to a non grow at all because such a tiny alloc is going to be reallocated soon (as a possible optimization in malloc() library) – Luis Colorado Jan 07 '16 at 14:09

2 Answers2

5

The debugger does not know that arra is any more than a single pointer. If you want the debugger to print out the contents of arra as an array, you need to walk the elements yourself, or cast it to an array type before printing.

jxh
  • 69,070
  • 8
  • 110
  • 193
0

First of all, how do you know that realloc has done nothing? Returning a different pointer is not warranted by realloc(3), as if possible it will try the realloc in place. Having said this, there's no way to know (externally to malloc) that it has done whatever you like or not, as the return value (being the same pointer) gives you no information about the new size.

By the way, how did you check if the sizes where the same or not. You don't show how you did in your code. Indeed, from your code you cannot get any information of the actual size returned by realloc (just see if the program crashed at all or not) If your used the sizeof operator, you are wrong, as you are using it with pointers and the size returned is always the same (the size of a pointer variable, an address) if you used the debugger, it has the same resources as the main program to check the size of whatever realloc returned (that is, nothing again) so, what is the reasoning to conclude that realloc is not working.

Next time, do several mallocs (not only two) and realloc all pointers to values much greater (to avoid optimizations leading to the same pointer returned) like this:

char *a = malloc(10), *b = malloc(10), *c = malloc(10);
char *aa = realloc(a, 10000), *bb = realloc(b, 10000), *cc = realloc(c, 10000);
if (a != aa || b != bb || c != cc) 
    printf("realloc *changed* the return values "
           "(a=%p, aa=%p, b=%p, bb=%p, c=%p, cc=%p)\n", 
             a,    aa,    b,    bb,    c,    cc);
else
    printf("realloc didn't move the return values\n");
Luis Colorado
  • 10,974
  • 1
  • 16
  • 31