On Fri, 15 Aug 2008, Chris Knadle wrote:
> On Friday 15 August 2008, lex wrote:
>> Any solution has to work with gotos present as well as for
>> structured code.
>
> There are a few rare instances in which gotos are necessary, but I'm sure
> you're aware that they're to be avoided otherwise.
>
>> Another example is where there is something like
>>
>> if (condition 1) { maybe set x } else if (c2) {maybe set x} else ...
>> if (cn) {...},
>> followed by a use of x.
>
> I'm assuming "x" is being used as a status flag. If that's the case,
> then "x" should have a default setting to start with. Is there a reason why
> giving "x" a default setting would be the wrong thing to do? [I read your
> initial thoughts on this, but I don't understand your reasoning.]
>
> If "x" is not a status variable, then what is it used for?
>
> -- Chris
>
> --
>
> Chris Knadle
-----------------------------------
x can be any program variable, like int, unsigned int, char *, or
a structure or class, in a C or C++ program.
The reason for not setting it to a default is that if it ever uses the
default then it is going to be an error. I already know this in advance.
E.g. at some same places in the logic, x is computed to be a function of
some other variables. For every unique combination of the other
variables, let's say that x is always different also.
If x is initialized, then if there are two or more combinations of other
variables where x is not set at the end, then no default value can be
chosen that is correct for all inputs. x is then used for more logic,
until the end.
Then is when the msg "uninitialized before use" tells you that you have a
problem.
Or, a simpler example. x is a number. The program first computes x
to be the sum of a bunch of input numbers, then it uses x for later
arithmetic.
If there is a path through the logic that fails to compute x, then no
default intermediate value of x can be correct for every combination
of inputs.
There is no default value that can be guaranteed to be equal to the
result of all possible functions.
Lex
_______________________________________________
Mid-Hudson Valley Linux Users Group http://mhvlug.org
http://mhvlug.org/cgi-bin/mailman/listinfo/mhvlug
Upcoming Meetings (6pm - 8pm) MHVLS Auditorium
Jun 4 - Sqeak! and eToys
Jul 2 - KVM (Tenative)
Aug 6 - Zenos
Sep 3 - TBD