exit() problem

Jun 8, 2010 at 5:14pm
i run the codes as follow:
1
2
3
4
5
if(length != np*sizeof(float)*6)
{
cout<<"The size of file "<<_lname.front()<<" is not right!"<< endl;
exit(EXIT_FAILURE);
}

but it does not show the message properly.

But if I ran as follow:
1
2
3
4
5
if(length != np*sizeof(float)*6)
{
cout<<"The size of file "<<_lname.front()<<" is not right!";
exit(EXIT_FAILURE);
}

it will show the message properly. I don't understand why does it happen.
Jun 8, 2010 at 5:15pm
closed account (S6k9GNh0)
It's not a problem with exit. It's a matter of the stream not flushing which means its still in pending. Using std::flush instead of std::endl will give a similar and proper result.
Jun 9, 2010 at 3:23pm
but I think endl is "\n"+flush. why it doesn't work?
Jun 9, 2010 at 4:23pm
I also tried <<flush. it also doesn't work. the message only show half.
Jun 9, 2010 at 4:34pm
You should post a compilable minimal example that exhibits the problem.
And what is shown exactly anyway?
Jun 9, 2010 at 4:40pm
I'm skeptical. Take Athar's advice and compile a small test program to verify that it works.

Are you sure it doesn't have to do with _lname.front()? Is it a string? Is it null terminated? This sounds like a memory issue.

Also, what on Earth are you doing here?
if(length != np*sizeof(float)*6)
Last edited on Jun 9, 2010 at 4:41pm
Jun 9, 2010 at 8:11pm
The file is quite big. I think the problem is quite easy to explain:
if I try
 
cout<<"The size of file "<<_lname.front()<<" is not right!"<<endl;

before exit(FAILURE) or retrun false; in Linux. Everything is alright.

But if I run it in MSVC 2008, sometime it can only show "The size of file " without the rest.
if I try
1
2
cout<<"The size of file "<<_lname.front()<<" is not right!"<<endl<<endl;
exit(FAILURE);
in MSVC. the result is alright.
if I try
1
2
cout<<"The size of file "<<_lname.front()<<" is not right!"<<endl;
//exit(FAILURE); 
the message shows correctly.
I think it's a problem of endl and exit function were put together in MSVC.
And if I try
1
2
cout<<"The size of file "<<_lname.front()<<" is not right!\n";
exit(FAILURE);
the message is alright.
Jun 9, 2010 at 8:30pm
Well, the problem is not with the code you posted, but somewhere else (invalid memory accesses or whatnot). Then again, hard to tell without even knowing what _lname is.
Jun 9, 2010 at 8:33pm
_lname is a list<string> which store many binary files' names
Jun 9, 2010 at 8:38pm
I add one file with wrong size to the list, so when the searching reach this file, the codes will execute these two lines.
Topic archived. No new replies allowed.