-
Quick Realloc question
Code:
strucType makeBigger(structType x, char ch)
{ /* Struct x was previous malloc to 8 bits*/
x = realloc(x, 100);
printf("%d", sizeof(x));
}
So this should print out 100, but it keeps printing out 8.. I know its something to do with pointers but I have nooo idea what. Have added *'s and &'s infront of the x's and nothing is working. Any help would be really appreciated :-)
-
It prints 8 because you are asking for the size of the pointer (x).
You can't actually print out the size of a block of allocated memory. You have to track that for yourself.
-
Oh!!! Ok, that makes sense... Thanks so much!
-
Quote:
Originally Posted by
Sonny Trujillo
Code:
strucType makeBigger(structType x, char ch)
{ /* Struct x was previous malloc to 8 bits*/
x = realloc(x, 100);
printf("%d", sizeof(x));
}
What happens to x when realloc fails?
Quzah.
-
Which is precisely why I recommend that people do not use realloc.
Virtually all of those who start threads on here and have code that includes realloc are using it wrong.