getline() and size_t

Michael Vaughn
Tue May 4 17:53:00 GMT 2010

I have an odd and somewhat dumb question. While using getline() for the first time, (with a pointer to NULL for lineptr), I accidentally passed int * instead of size_t* for the second argument. What was surprising to me is that this somehow causes corruption of the allocated heap data. When I go to free the buffer, I get the classic "double free or corruption" message and dump.
I'm not entirely certain how the type mix-up could cause such wide-ranging issues when getline() goes to allocate the buffer, (especially considering I was using a positive 4-bit integer, and size_t is 8 bits so I wouldn't expect there to be, for example, a sign extension issue.)
Obviously, using int * is wrong, but I was wondering if anyone could clarify what goes on when I mix up size_t and int in this context. [For my peace of mind - something about a memory corruption issue that I don't fully understand, even if it is seemingly rectified worries me].
Thank you for your patience,
 Michael Vaughn

More information about the Libc-help mailing list