On Fri, 15 Aug 2008, lex wrote:
>
>
> On Fri, 15 Aug 2008, Mike Kershaw wrote:
>
>> On Fri, Aug 15, 2008 at 12:03:23PM -0400, lex wrote:
>>>> The first, and all subsequent before a write, read operations.
>>>> (compare, print, assign to another var, etc)
>>>>
>>>> -m
>>>>
>>>> --
>>>> Mike Kershaw/Dragorn <[EMAIL PROTECTED]>
>>>> GPG Fingerprint: 3546 89DF 3C9D ED80 3381 A661 D7B2 8822 738B BDB1
>>>>
>>>> Life is a whim of several billion cells to be you for a while.
>>>>
>>>
>>> No, my program does not have any reads nor writes nor any other I/O.
>>>
>>> It has 45 uses of x. Only one or two use the variable without it having
>>> been set first. Which ones are they?
>>
>> That doesn't really make any sense.
>>
>> The warning is generated for any read operations before the variable is
>> set. Any operations AFTER the variable is assigned will NOT generate
>> the warning in that function, since the content has now been
>> initialized. If you do one read op and 44 write ops, you'll get the
>> error for the first one. If you do 10 read ops before initialization,
>> then the warning applies to the first 10. After you put a value in
>> there, it's initialized, and the error is no longer applicable.
>>
>> Notice I said read *operation*. Anything that reads from the variable.
>> if (x == 1) is a read operation. printf("%d", x); is a read operation.
>> y = x; is a read operation. Reading the contents of an unitialized
>> variable will generate that warning - it's got nothing to do with user
>> or file i/o.
>>
>> Any uses of X before you assign a value to it are unitialized.
>>
>> While you can solve it by simply adding an init during the variable
>> creation (int x = 0;) that won't solve the logic errors of using a
>> variable that has no content before you put something into it.
>>
>> While there are some obtuse ways to make it so the variable really does
>> have legit content in it and the compiler thinks it doesn't, it's
>> generally a coding error.
>>
>> -m
>>
>> --
> Yes, there may be 10 reads before it is set, and 20 therafter.
> But which ones are the 10? The compiler does not give that information,
> only the line number of where the variable is declared.
>
> Lex
When you say BEFORE, the sets and uses are not neccessarily executed in
the order in which they appear in the code, if the code has gotos in it.
Any solution has to work with gotos present as well as for structured
code.
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.
Let's say that all of the possible conditions may result in a set of x,
but not necessarily. Which ones are the possible problems?
Some of the "maybe set x" sequences may be pages long, with complex
conditions themselves.
The compiler does correctly analyze this and reports a warning msg,
but does not identify which ones may have a problem.
I don't ever bother trying to test a program with the warning msg present,
because, as you say, it is almost always an error which will cause a
problem later on.
I have two solutions to this problem. I think I can see that no one has a
ready to use automated solution yet. Definitely not any of the 4 or
500 pages I found through the search engines with differing queries.
1. The souce only solution:
o With a program, find all of the occurrences of x that are possible
valid variable names. Skip non executable sections if it is easy to do.
if not, it will still work, but take longer.
o Also in a program, identify which of the references are declares, and
get the declaration type, and add a declaration for a variable called
x_default, where x is the variable name, and be sure to initialize it to
something.
o For all of the references that are possible uses, have the program
substitute x_default for the variable x. Uses are easy to distinguish
from sets because they have an equal sign in front of the variable.
o Then the pgm should compile without the warning msgs. If not, then the
places where x_default are present are not the offending places, and
I don't know why that would happen.
o Make one copy of the source code, C1, C2, ... Cn, and for Ci replace
"x_default" by "x".
o Compile all Cn copies of the code and save the warning msgs in file Wn.
o Each Wn that has the msg for x identifies that the nth occurrence of
x in the source file is where there is a problem. So process all of them
and print out the all n where Wn has the msg. These are where the
possible problems are.
This is good, but it still does not tell me what are the possible paths
through the code that can result in a use without a set.
To find these, I have to depend on adding to the source code something
that prints the line number of each line to a trace file as it executes,
e.g. something like "q(x);", which prints the value of x and the line
number.
Make sure that the x_default value is set to something that will not
be a valid value, if possible.
Replace every use of x_default by a function call fx_default(x_default),
which checks to see if the value is the default value, and if so prints
the line number to the trace output and stops, otherwise just returns
the input value, so execution continues normally.
This is also done by the program tool, not manually. I don't want to have
to waste any time doing any actual work myself, editing the code, etc..
When the use of the default value is found, the prior trace sequence
contains program name and line numbers of the exact path through the
logic, no matter how complex, that led to the use of the unset variable.
In case I am working on a problem in a program that generates billions
of lines of trace output before getting to the offending case, I pipe
the trace output to a filter program that only remembers the last N lines
of trace output, and can display them on request.
This method works even when the program runs for hours before failing,
and is useable where no debug tool depending on stepping and breakpoints
even has a chance of being useable.
Are there any better answers? If so, please tell me now.
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