"AC Slater" <> wrote in message
news:f%oQb.11495$ et...
>
> "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.
>
>
>