Since buffer is a pointer (not an array), the sizeof operator returns the size of the pointer, not the size of the buffer that it points to. There is no standard way to determine this size, so you need to keep accounting yourself (i.e. Remember how much you allocated.)
BTW, this is the same for
char *p = "hello, world\n";
Interesting that
sizeof "hello, world\n"
equals 14. Can you guess why?
Jens
source share