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");