Newbie : Reading input into character buffer (am I doing this right?)

Discussion in 'C++' started by deech, Feb 12, 2010.

  1. deech

    deech Guest

    Hi all,
    I am reading characters from input using 'cin' into a character
    buffer. I want to do it dynamically and I wrote some code to do so,
    and it seems to work, but I am concerned that I am doing something
    that is undefined. Here is the code :
    int main () {
    char * buffer = new char;
    char * iterator = buffer;
    int buffer_length = 0;
    while ((*iterator = cin.get()) != EOF) {
    iterator++;
    buffer_length++;
    }
    *iterator = '\0';
    cout << "Buffer Contents : " << buffer << endl;
    cout << "Buffer length : " << strlen(buffer) << endl;
    }

    All the examples I've seen ask you to predetermine the number of
    characters that are going to be entered, and store them in a fixed-
    size char array. But this seems to work, and is more flexible. K&R
    tells me that modifying the contents of a pointer leads to undefined
    behavior.

    Is this a good way of doing what I want?
    thanks ...
    -deech
     
    deech, Feb 12, 2010
    #1
    1. Advertising

  2. Re: Newbie : Reading input into character buffer (am I doing thisright?)

    deech wrote:
    > I am reading characters from input using 'cin' into a character
    > buffer. I want to do it dynamically and I wrote some code to do so,
    > and it seems to work, but I am concerned that I am doing something
    > that is undefined.


    Unfortunately, yes.

    > Here is the code :
    > int main () {
    > char * buffer = new char;


    So, you allocate only *one* char. Not ten, not a hundred, not a
    thousand. One.

    > char * iterator = buffer;


    You make your 'iterator' point to the only char you've allocated.

    > int buffer_length = 0;
    > while ((*iterator = cin.get()) != EOF) {
    > iterator++;


    You increment your iterator. Now where does it point? And on the next
    iteration, where will the character you read be stored?

    > buffer_length++;
    > }
    > *iterator = '\0';


    Again, you've moved past the boundaries of the memory you have
    allocated, and you're trying to store something there. Undefined behaviour.

    > cout << "Buffer Contents : " << buffer << endl;
    > cout << "Buffer length : " << strlen(buffer) << endl;
    > }
    >
    > All the examples I've seen ask you to predetermine the number of
    > characters that are going to be entered, and store them in a fixed-
    > size char array.


    And for a good reason, too.

    > But this seems to work, and is more flexible.


    Flexible? Like using-ropes-for-aircraft-wings flexible?

    > K&R
    > tells me that modifying the contents of a pointer leads to undefined
    > behavior.


    I don't know what you're referring to, and I don't think K&R ever wrote
    much about C++.

    > Is this a good way of doing what I want?


    No. See 'std::getline' function. And if you don't yet grok dynamic
    memory and the responsibility that comes with its use, don't use it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 12, 2010
    #2
    1. Advertising

  3. Re: Newbie : Reading input into character buffer (am I doing thisright?)

    On Feb 12, 4:20 pm, deech <> wrote:
    > Hi all,
    > I am reading characters from input using 'cin' into a character
    > buffer.


    How many characters do you want to read? A fixed number, up to the
    next whitespace, up to the next end of line. Up to end of file?

    > I want to do it dynamically and I wrote some code to do so,
    > and it seems to work, but I am concerned that I am doing something
    > that is undefined. Here is the code :
    > int main () {
    >   char * buffer = new char;


    You allocate a single character here.

    >   char * iterator = buffer;
    >   int buffer_length = 0;
    >   while ((*iterator = cin.get()) != EOF) {
    >     iterator++;


    Oops! Here you increment your buffer pointer past the single
    allocated character. This triggers undefined behavior and will
    normally result in a crash sooner or later.

    >     buffer_length++;
    >   }
    >   *iterator = '\0';
    >   cout << "Buffer Contents : " << buffer << endl;
    >   cout << "Buffer length : " << strlen(buffer) << endl;
    >
    > }


    You can read up to the end of line like this:

    std::string line;
    std::getline(cin, line);

    Or the entire file like this:

    std::string text;
    std::copy(std::istream_iterator<char>(cin),
    std::istream_iterator<char>(), std::back_inserter(text));

    Note that the string class now takes care of buffer management for
    you.
     
    Gert-Jan de Vos, Feb 12, 2010
    #3
  4. deech

    deech Guest

    Re: Newbie : Reading input into character buffer (am I doing thisright?)


    > How many characters do you want to read? A fixed number, up to the
    > next whitespace, up to the next end of line. Up to end of file?


    Until I hit Ctrl-D (on Linux).


    > You can read up to the end of line like this:
    >
    > std::string line;
    > std::getline(cin, line);
    >
    > Or the entire file like this:
    >
    > std::string text;
    > std::copy(std::istream_iterator<char>(cin),
    > std::istream_iterator<char>(), std::back_inserter(text));
    >
    > Note that the string class now takes care of buffer management for
    > you.


    I didn't use C++ string facility because I was trying to understand
    dynamic memory a little better.

    I appreciate you explaining my mistake.

    -deech
     
    deech, Feb 12, 2010
    #4
  5. deech

    James Kanze Guest

    Re: Newbie : Reading input into character buffer (am I doing thisright?)

    On Feb 12, 3:55 pm, Victor Bazarov <> wrote:
    > deech wrote:


    [...]
    > > K&R tells me that modifying the contents of a pointer leads
    > > to undefined behavior.


    > I don't know what you're referring to, and I don't think K&R ever wrote
    > much about C++.


    And they certainly never said that modifying the contents of a
    pointer leads to undefined behavior.

    --
    James Kanze
     
    James Kanze, Feb 12, 2010
    #5
  6. deech

    deech Guest

    Re: Newbie : Reading input into character buffer (am I doing thisright?)


    > And they certainly never said that modifying the contents of a
    > pointer leads to undefined behavior.


    I may have misread, but I saw it on pg104 in the 4th paragraph
    starting with "amessage is an array ..."

    -deech
     
    deech, Feb 12, 2010
    #6
  7. deech

    Default User Guest

    deech wrote:

    >
    > > And they certainly never said that modifying the contents of a
    > > pointer leads to undefined behavior.

    >
    > I may have misread, but I saw it on pg104 in the 4th paragraph
    > starting with "amessage is an array ..."


    Here's the full text:

    There is an important difference between these definitions:

    char amessage[] = "now is the time"; /* an array */
    char *pmessage = "now is the time"; /* a pointer */

    amessage is an array, just big enough to hold the sequence of
    characters and '\0' that initializes it. Individual characters within
    the array may be changed but amessage will always refer to the same
    storage. On the other hand, pmessage is a pointer, initialized to point
    to a string constant; the pointer may subsequently be modified to point
    elsewhere, but the result is undefined if you try to modify the string
    contents.



    What is undefined is trying to modify a string literal (they called it
    a constant).

    Something like:

    char *pmessage = new char[20];

    pmessage[0] = 'A';


    Would be fine.



    Brian
     
    Default User, Feb 12, 2010
    #7
    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. Tom
    Replies:
    0
    Views:
    429
  2. KwikRick
    Replies:
    1
    Views:
    359
    Christos TZOTZIOY Georgiou
    Aug 22, 2003
  3. Replies:
    4
    Views:
    355
    Default User
    Sep 6, 2006
  4. A. Farber
    Replies:
    8
    Views:
    579
    Mike Amling
    Oct 20, 2009
  5. Navaneeth
    Replies:
    29
    Views:
    936
    Jorgen Grahn
    Aug 28, 2010
Loading...

Share This Page