ifstread read

A

AC Slater

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
 
G

Gianni Mariani

AC said:
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.
 
A

AC Slater

Gianni Mariani said:
AC said:
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.
 
M

Mike Wahler

AC Slater said:
Gianni Mariani said:
AC said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top