Search code examples
crealloc

Problems with realloc in C


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!


Solution

  • 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.