Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ifstread read

Reply
Thread Tools

ifstread read

 
 
AC Slater
Guest
Posts: n/a
 
      01-23-2004
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



 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      01-23-2004
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.



 
Reply With Quote
 
 
 
 
AC Slater
Guest
Posts: n/a
 
      01-24-2004

"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:buscer$(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-25-2004

"AC Slater" <(E-Mail Removed)> wrote in message
news:f%oQb.11495$(E-Mail Removed) et...
>
> "Gianni Mariani" <(E-Mail Removed)> wrote in message
> news:buscer$(E-Mail Removed)...
> > 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.
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Program to read from EOR end of last read? snoopy_@excite.com Java 3 04-07-2006 12:32 PM
Binary data stored in SQL Server: can't read from ASP.NET, *can* read from Access? Doug ASP .Net 3 11-04-2005 07:35 PM
Re: Unable to read video DVDs and can read Data DVDs Biz DVD Video 0 07-22-2005 03:44 AM
How much was read during istream::read ? Steve C++ 6 05-13-2004 05:54 PM
Re: How to change Read Only Constraint to Read-Write Isaac VHDL 0 07-10-2003 01:43 PM



Advertisments