Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE
From: Tom Widmer (tom_usenet_at_hotmail.com)
Date: 02/17/05
- Next message: Tom Widmer: "Re: Howto inherit from a templated class?"
- Previous message: Tom Widmer: "Re: how to quickly initilize a struct component?"
- In reply to: Norm Dresner: "Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE"
- Next in thread: Norm Dresner: "Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE"
- Reply: Norm Dresner: "Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 17 Feb 2005 12:10:42 +0000
Norm Dresner wrote:
> and everyone agrees that there's nothing wrong with the code as written, but
> the program crashes with an access violation on the fgets() line. aux is
> declared as char[1025] a few lines above and right-clicking in the debugger
> just before the fgets() call is executed confirms this.
Well, there is one minor problem with the code. The fgets call should be:
fgets( aux , sizeof aux , stdin );
since fgets only reads up to N-1 characters anyway, so in your code you
were reading only up to 1023 characters, not the 1024 that your array
had space for.
> NEW INFORMATION: If I replace fgets( aux ... ) with gets( aux ) the program
> runs perfectly!!!!! This means, at least to me, that there's really nothing
> wrong with the "code" or with the "stdin" input stream because that's what
> gets() uses. ALSO ... I started a brand-new console-app project which
> contains just the following code:
>
> #include <libc.h /* this is my own */
> /* include-just-about */
> /* -everything-I'll-need */
> /* header file that's also */
> /* included in the */
> /* previous program too */
>
> int main(int argc, char* argv[])
> {
> char aux[1025];
> printf("What's up doc? ");
> fgets( aux , sizeof( aux ) - 1 , stdin );
> printf( "\n\nYou entered \"%s\"\n" , aux );
> return 0;
> }
>
> and this works just fine. SO ... There's obviously something in the way the
> first program is setup -- a library or something -- that causes it to crash.
Crashes in code that looks fine in isolation can happen for a number of
reasons.
> This is the only place in the program that stdin is used or any input from
> the console is required. What should I be looking for? Could I possibly
> be trashing some pointer somewhere?
That is the most likely explanation - you may have corrupted the stack
elsewhere in the program. This would lead generally to a crash at some
pointer after the corruption occurred. Have you checked for obvious
stack corrupters like returning pointers or references to local
variables? How much code executes before the fgets call?
I don't quite see how if gets()
> works because it looks like it calls getchar() which does fgetc() from
> stdin itself.
If you do have a corrupted stack, then the explanation might be
incredibly complicated, but that's the nature of undefined behaviour -
pretty much anything might happen.
> Since I'm likely to be the only one using this program -- although a trusted
> co-worker with equally high and obscure security clearance might be too
> eventually -- should I just shrug my shoulders and leave the gets() call in
> place or am I just deferring the problem until later?
You are probably just deferring the problem, since it is unlikely that
changing to gets really fixes things. In the worst case, the program
will appear to run correctly by will generate subtley incorrect output.
Tom
- Next message: Tom Widmer: "Re: Howto inherit from a templated class?"
- Previous message: Tom Widmer: "Re: how to quickly initilize a struct component?"
- In reply to: Norm Dresner: "Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE"
- Next in thread: Norm Dresner: "Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE"
- Reply: Norm Dresner: "Re: stdin ? stdin ? What's wrong with stdin? -- an UPDATE"
- Messages sorted by: [ date ] [ thread ]