getline() issue.

This is a discussion on getline() issue. within the Linux Programming forums, part of the Platform Specific Boards category; I was working on a program that used getline() in the GNU C library, and I had a strange question. ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    getline() issue.

    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.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    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.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    3
    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.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    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*.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 05:42 PM
  4. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21