You're being quite pedantic today ;-) I'm not advocating keeping malloc'ed memory around, I'm just telling the OP that it's bad practice...
QuantumPete
Printable View
>You're being quite pedantic today ;-)
I'm pedantic every day. I just managed to hit you twice in quick succession. ;)
>I'm not advocating keeping malloc'ed memory around
I know. Your advice was sound, but the "not strictly necessary" part could cause confusion that we've had to squelch before and I doubt anyone is keen on doing it again.
Didn't try to keep program running, it's just that Salem gave ma an advice to try install some other compiler, i'm using Visual 6.0, and try to run program there. So i installed Dev, and it had already that "things" written so...I really even don't know what system"PAUSE" was until now :)Quote:
>system("PAUSE");
This is probably the worst possible way of keeping your program running. It has serious portability and security risks, so you should be using something more like this:
But, Thank you, I appreciate this
as long as we're being pedantic, shouldn't it be:given that you probably already went to EOF?Code:if(!feof(stdin)) getchar();
>as long as we're being pedantic, shouldn't it be
At this point we don't care unless the implementation denies further calls after an "error" state (mine does this, and so do C++ iostreams). But, unless it's a serious error we can probably avoid or ignore it. So something like this would be more portable:
However, that's a tad excessive as legitimate stream errors are rare. Maybe something more sedate:Code:int main ( void )
{
/* ... */
jsw_flush ( stdin, '\n' );
if ( ferror ( stdin ) ) {
/* Handle the error */
}
else {
/* We don't really care about EOF at this point */
clearerr ( stdin );
fputs ( "Press Enter to continue . . .", stdout );
fflush ( stdout );
getchar();
}
}
Or you can do as my FAQ example and include clearerr inside of jsw_flush.Code:int main ( void )
{
/* ... */
jsw_flush ( stdin, '\n' );
clearerr ( stdin );
fputs ( "Press Enter to continue . . .", stdout );
fflush ( stdout );
getchar();
}
>given that you probably already went to EOF?
Actually, it's more likely with stdin to stop at a newline and only hit EOF if end-of-file was explicitly signaled or the stream is being redirected to a file. But if we're using this to pause the program before termination, the redirection excuse isn't very convincing, and the whole purpose of jsw_flush is to remove extraneous characters that we know are present on a system that line buffers input. ;)
As long as we're being pedantic, this whole operation is far from as simple as it seems on the surface, and what's been shown is merely a common solution that works most of the time.
I actually wrote a C++ version of this recently that digs pretty far into standard iostreams and shows how "flushing" the input stream is surprisingly difficult to get right for all cases.
works for me :)
thanks for the explanation.