ifstread read

Discussion in 'C++' started by AC Slater, Jan 23, 2004.

  1. AC Slater

    AC Slater Guest

    In regards to the following code:

    char tmp[6];

    myifstread.read(tmp,5);

    Does tmp[5] = '\0' by definition? E.g. does .read put the null terminator?
    If not, why would it be that for months the print statement after the read
    would show just 5 characters than out of nowhere sometimes it shows 5 chars
    and then some garbage?

    Just trying to understand whats going on here.

    Frank
     
    AC Slater, Jan 23, 2004
    #1
    1. Advertising

  2. AC Slater wrote:
    > In regards to the following code:
    >
    > char tmp[6];
    >
    > myifstread.read(tmp,5);
    >
    > Does tmp[5] = '\0' by definition?


    (in this case, only if tmp was declated as statically initialized (not
    auto or dynamically).

    E.g. does .read put the null terminator?

    No.


    > If not, why would it be that for months the print statement after the read
    > would show just 5 characters than out of nowhere sometimes it shows 5 chars
    > and then some garbage?


    Because if you allocated automatic storage, the contents of the array is
    undefined and in most implementations "random" garbage will be in an
    array declared like that. You happened to be unlucky that you didn't
    find it during yor development cycle.

    >
    > Just trying to understand whats going on here.


    Unitialized variable. If you develop on linux x86 boxen, you can use
    valgrind to detect these problems ... Julian Seward (valgrind
    mastermind) rocks. You can also use purify.
     
    Gianni Mariani, Jan 23, 2004
    #2
    1. Advertising

  3. AC Slater

    AC Slater Guest

    "Gianni Mariani" <> wrote in message
    news:buscer$...
    > AC Slater wrote:
    > > In regards to the following code:
    > >
    > > char tmp[6];
    > >
    > > myifstread.read(tmp,5);
    > >
    > > Does tmp[5] = '\0' by definition?

    >
    > (in this case, only if tmp was declated as statically initialized (not
    > auto or dynamically).
    >
    > E.g. does .read put the null terminator?
    >
    > No.
    >
    >
    > > If not, why would it be that for months the print statement after the

    read
    > > would show just 5 characters than out of nowhere sometimes it shows 5

    chars
    > > and then some garbage?

    >
    > Because if you allocated automatic storage, the contents of the array is
    > undefined and in most implementations "random" garbage will be in an
    > array declared like that. You happened to be unlucky that you didn't
    > find it during yor development cycle.
    >
    > >
    > > Just trying to understand whats going on here.

    >
    > Unitialized variable. If you develop on linux x86 boxen, you can use
    > valgrind to detect these problems ... Julian Seward (valgrind
    > mastermind) rocks. You can also use purify.
    >


    You are correct; in the real code the variable tmp is allocated dynamically
    with new.

    Its very weird though; this program must have run 100 times w/ no issue
    before it started displaying this issue.

    Thanks for the help.
     
    AC Slater, Jan 24, 2004
    #3
  4. AC Slater

    Mike Wahler Guest

    "AC Slater" <> wrote in message
    news:f%oQb.11495$...
    >
    > "Gianni Mariani" <> wrote in message
    > news:buscer$...
    > > AC Slater wrote:
    > > > In regards to the following code:
    > > >
    > > > char tmp[6];
    > > >
    > > > myifstread.read(tmp,5);
    > > >
    > > > Does tmp[5] = '\0' by definition?

    > >
    > > (in this case, only if tmp was declated as statically initialized (not
    > > auto or dynamically).
    > >
    > > E.g. does .read put the null terminator?
    > >
    > > No.
    > >
    > >
    > > > If not, why would it be that for months the print statement after the

    > read
    > > > would show just 5 characters than out of nowhere sometimes it shows 5

    > chars
    > > > and then some garbage?

    > >
    > > Because if you allocated automatic storage, the contents of the array is
    > > undefined and in most implementations "random" garbage will be in an
    > > array declared like that. You happened to be unlucky that you didn't
    > > find it during yor development cycle.
    > >
    > > >
    > > > Just trying to understand whats going on here.

    > >
    > > Unitialized variable. If you develop on linux x86 boxen, you can use
    > > valgrind to detect these problems ... Julian Seward (valgrind
    > > mastermind) rocks. You can also use purify.
    > >

    >
    > You are correct; in the real code the variable tmp is allocated

    dynamically
    > with new.


    "allocate" does not mean "initialize".

    int main()
    {
    char *p = new char[6];

    *p; /* undefined behavior, the allocated memory has not
    been initialized or assigned a valid value */
    }

    > Its very weird though; this program must have run 100 times w/ no issue
    > before it started displaying this issue.


    Undefined behavior is weird by definition. :)

    >
    > Thanks for the help.
    >
    >
    >
     
    Mike Wahler, Jan 25, 2004
    #4
    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. Isaac
    Replies:
    0
    Views:
    2,477
    Isaac
    Jul 10, 2003
  2. Doug
    Replies:
    3
    Views:
    4,404
    Bruce Barker
    Nov 4, 2005
  3. Replies:
    3
    Views:
    543
    John W. Krahn
    Apr 7, 2006
  4. Steve
    Replies:
    6
    Views:
    3,705
    Steve
    May 13, 2004
  5. lokb
    Replies:
    5
    Views:
    4,685
    Victor Bazarov
    Jul 8, 2004
Loading...

Share This Page