View Full Version : getline() issue.

05-04-2010, 11:28 AM
I was working on a program that used getline() in the GNU C library, and I had a strange question. I screwed up and used int* as my second argument, instead of size_t *. Somehow, if I used a pointer to int in lieu of size_t, I get memory corruption later on. Changing the second parameter to the appropriate type fixes the issue, but I'm somewhat confused as to how a change that subtle would lead to wider issues down the line.

05-04-2010, 11:32 AM
Most likely because int and size_t are of different size. Had you passed an ssize_t (same size but signed), then it wouldn't have crashed.

05-04-2010, 11:39 AM
I was thinking that, but the integer is well under 4 bytes and unsigned, so sign extension shouldn't be an issue either. (Unless casting from size_t to int is more than just truncation)

PS: Can anyone point me towards some sort of mailing list for people programming with GLIBC? I've been asking the all-knowing Google, but I'm coming up with nothing.

05-04-2010, 11:41 AM
I'm not talking about sign extension. Try out and check what is `sizeof(int)' and `sizeof(size_t)'; if the sizes differ, then you can't pass and int* when a function is expecting a size_t*.

05-04-2010, 12:06 PM
Oh! Right! I get you! So if I, for example, pass a pointer to int off my stack, it's going to attempt to dereference it as a pointer to size_t (in my case, size_t is long long int), which means it's going to have some junk appended to one end or the other, depending on the endianness of my architecture and the contents of the stack adjacent to the int.