I just don't get it, really, I don't getline(char*,streamsize);

Discussion in 'C++' started by jalkadir, Oct 5, 2005.

  1. jalkadir

    jalkadir Guest

    I am trying to get character string from the user, to do that I use
    getline(char_type*, streamsize), but I get a segmentation fault??!!

    Can anyone give me a hand, what am I doing wrong?

    --snip
    char* cstr;

    std::cout << "House/Appartment # ";
    std::cin.getline(cstr, CHAR_MAX); //<<==seg fault


    TIA
     
    jalkadir, Oct 5, 2005
    #1
    1. Advertising

  2. jalkadir

    Mike Wahler Guest

    "jalkadir" <> wrote in message
    news:...
    >I am trying to get character string from the user, to do that I use
    > getline(char_type*, streamsize), but I get a segmentation fault??!!
    >
    > Can anyone give me a hand, what am I doing wrong?
    >
    > --snip
    > char* cstr;
    >
    > std::cout << "House/Appartment # ";
    > std::cin.getline(cstr, CHAR_MAX); //<<==seg fault


    It's not 'getline()' that you don't get, your misunderstanding
    is much more fundamental. 'getline()'s first argument is a
    pointer to char, but that pointer needs to point to some
    (sufficient number of contiguous) characters, i.e. an array
    of characters. You have only provided a pointer, but failed
    to cause it to point anywhere. It's the fact that 'getline()'
    will dereference this pointer that causes the problem in your
    code. Dereferencing an invalid or NULL pointer gives 'undefined
    behavior', where *anything* could happen. Consider yourself
    fortunate that this time the problem is visible, as a 'segmentation
    fault'. THe language makes no requirement at all what the behavior
    would be. Equally possible would be a failure with no visible
    indications.

    Perhaps part of your trouble is that you've fallen into a very
    common novice error: not distinguishing between a pointer an
    an array. A pointer is not an array. An array is not a pointer.
    A type 'char*' object is *not* a string (although it can be made
    to point to one (A 'c-style string, that is)).

    Also, your (attempted) use of 'C-style' strings, with all the attendant
    potential for errors, is unnecessary. Use the std::string class for
    strings.
    That's what it's for.

    std::string cstr; // automatically is a well-defined object with a
    // valid value (in this case, an empty string)

    std::cout << "House/Apartment # ";
    std::getline(std::cin, cstr);

    'std::getline()' is declared by <string>. Note the two major differences
    from 'istream::getline()': It's a nonmember function, and it stores
    characters in a std::string object rather than a character array.
    A std::string object manages its own memory, so you can't run into
    a problem like you did with an uninitialized 'char*'.

    A final note about your code: You also seem to be misunderstanding
    the meaning and purpose of the 'CHAR_MAX' macro. It's defined as
    returning a value which is the largest possible value for a single
    character object. It has nothing to do with a count of characters
    or stream operations.

    Which C++ book(s) are you reading?

    -Mike
     
    Mike Wahler, Oct 5, 2005
    #2
    1. Advertising

  3. jalkadir

    Guest

    the "cstr" variable you defined (confusing name btw.) is a pointer to a
    character. However, there is no memory allocated for it to point to.

    You have two options.

    a) Define cstr as "char cstr[ CHAR_MAX ];"
    b) Allocate memory for it "char* cstr = new char[ CHAR_MAX ];"

    In case b), don't forget to free the memory later "delete [] cstr;"

    Even better would be to use std::string.

    Cheers,
    Andre
     
    , Oct 5, 2005
    #3
    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 Vinokur
    Replies:
    1
    Views:
    4,005
    Alex Vinokur
    Feb 12, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,056
    Ian Collins
    May 9, 2006
  3. puzzlecracker
    Replies:
    3
    Views:
    1,774
    Mike Wahler
    May 8, 2006
  4. Notebooker
    Replies:
    9
    Views:
    4,804
    Notebooker
    Jan 27, 2007
  5. Obnoxious User

    what is "streamsize" and "width(n)"?

    Obnoxious User, Jun 17, 2007, in forum: C++
    Replies:
    5
    Views:
    362
Loading...

Share This Page