Re: program bug
- From: Robert Gamble <rgamble99@xxxxxxxxx>
- Date: Fri, 2 May 2008 15:28:04 -0700 (PDT)
On May 2, 6:16 pm, "Bill Cunningham" <nos...@xxxxxxxxx> wrote:
"Robert Gamble" <rgambl...@xxxxxxxxx> wrote in message
news:2d73e006-fc0d-4ed7-ae1e-43535dea6140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
a is not initialized, referencing its value invokes undefinedbehavior.
a=fgetc(ifp);
fputc(a,ofp);
In the beginning a was declared. Is it not enough in this case to simply
declare an int? Or should I have done this int a=0; at the beginning of the
program?
You need to store a value into a variable before you use the value of
that variable, either through initialization (implicit or explicit) or
assignment. If you don't do this then the variable may contain
garbage ("indeterminate value" in standards parlance), that garbage
may be a trap value which will invoke UB when read. Note that the
alternative I provided does not have this problem because "a" is
assigned before its value is used.
--
Robert Gamble
.
- References:
- program bug
- From: Bill Cunningham
- Re: program bug
- From: Robert Gamble
- Re: program bug
- From: Bill Cunningham
- program bug
- Prev by Date: Re: program bug
- Next by Date: Re: program bug
- Previous by thread: Re: program bug
- Next by thread: Re: program bug
- Index(es):
Relevant Pages
|