getline - Segmentation fault

Discussion in 'C++' started by mamboknave@gmail.com, Apr 30, 2009.

  1. Guest

    From the keyboard I enter just one character and push Return key.
    Then this getline goes in Segmentation fault.
    Why so???

    char *const SL = "";
    parse_usr_data(SL);
    ....
    void parse_usr_data(char *SL)
    {
    ....
    std::cin.getline(SL,2);
    ....
    }
     
    , Apr 30, 2009
    #1
    1. Advertising

  2. Sjouke Burry Guest

    wrote:
    > From the keyboard I enter just one character and push Return key.
    > Then this getline goes in Segmentation fault.
    > Why so???
    >
    > char *const SL = "";
    > parse_usr_data(SL);
    > ...
    > void parse_usr_data(char *SL)
    > {
    > ...
    > std::cin.getline(SL,2);
    > ...
    > }

    Because you put 3 chars in a string with length 1??
     
    Sjouke Burry, Apr 30, 2009
    #2
    1. Advertising

  3. Default User Guest

    wrote:

    > From the keyboard I enter just one character and push Return key.
    > Then this getline goes in Segmentation fault.
    > Why so???
    >
    > char *const SL = "";
    > parse_usr_data(SL);
    > ...
    > void parse_usr_data(char *SL)
    > {
    > ...
    > std::cin.getline(SL,2);
    > ...
    > }


    SL points to a string literal, which has a size of one. For purposes of
    backward compatibility, a pointer to such a string literal can be
    assigned to a pointer to char. That, however, should never be done, as
    it is not modifiable storage. So you have two problems.

    1. You pass in this pointer to parse_usr_data(), which then attempts to
    modify the buffer pointed to. That is undefined behavior.

    2. Even if SL was modifiable, it can only hold one character.


    You should be std::string rather than char*, and the verison of getline
    that is not a member of the instream class:

    istream& std::getline( istream& is, string& s, char delimiter = '\n' );





    Brian
     
    Default User, Apr 30, 2009
    #3
  4. * Jeff Schwab:
    > Jeff Schwab wrote:
    >> wrote:

    >
    >>> char *const SL = "";

    >>
    >> You're not allowed to write to overwrite that string.

    >
    >> Your compiler should have warned you about this.

    >
    > To my surprise, g++ does not warn about this conversion unless
    > -Wwrite-strings is explicitly specified. -Wall doesn't cut it.


    Thank you.

    One more to add to that bunch of options...


    Cheers,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, Apr 30, 2009
    #4
  5. Guest

    Thanks to everyone. I may be stubborn but I've hard time to get it.
    The excuse is that it's just a few weeks I' fighting with C & C++.

    Anyway, I changed the declaration to:

    char Sl[1];

    and everything works fine.

    Yes, I'm using g++.
     
    , May 1, 2009
    #5
  6. * :
    > Thanks to everyone. I may be stubborn but I've hard time to get it.
    > The excuse is that it's just a few weeks I' fighting with C & C++.
    >
    > Anyway, I changed the declaration to:
    >
    > char Sl[1];
    >
    > and everything works fine.


    Why don't you read the replies in the thread.


    > Yes, I'm using g++.


    That does not matter.


    Cheers & hdh.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, May 1, 2009
    #6
  7. Default User Guest

    wrote:

    > Thanks to everyone. I may be stubborn but I've hard time to get it.
    > The excuse is that it's just a few weeks I' fighting with C & C++.
    >
    > Anyway, I changed the declaration to:
    >
    > char Sl[1];
    >
    > and everything works fine.


    Once again, you have a character array that can hold exactly one
    character. That did not at all seem to be what you wanted. I doubt very
    much that it is "working fine". It's just not as obviously broken as it
    was before.





    Brian
     
    Default User, May 1, 2009
    #7
  8. Jorgen Grahn Guest

    On Thu, 30 Apr 2009 18:50:13 -0400, Jeff Schwab <> wrote:
    > Jeff Schwab wrote:
    >> wrote:

    >
    >>> char *const SL = "";

    >>
    >> You're not allowed to write to overwrite that string.

    >
    >> Your compiler should have warned you about this.

    >
    > To my surprise, g++ does not warn about this conversion unless
    > -Wwrite-strings is explicitly specified. -Wall doesn't cut it.


    Earlier gcc versions (like 4.1) worked like that, but 4.3 is the other
    way around. You don't have to specify *any* options to get that
    particular warning.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, May 2, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    894
  2. Pud
    Replies:
    0
    Views:
    605
  3. Replies:
    0
    Views:
    570
  4. Ivan Vecerina
    Replies:
    0
    Views:
    506
    Ivan Vecerina
    Jun 29, 2003
  5. jan247
    Replies:
    6
    Views:
    411
    M. Nejat AYDIN
    Dec 27, 2005
Loading...

Share This Page