Question: Is this the way visibility and liftime work?
Scott Drellishak
c60c-4ab at e260-4c.berkeley.edu
Fri Oct 20 06:38:03 AEST 1989
I have recently, for a project in one of my classes, written a simple program
to make queries on a database. It read tokens from stdin, and processed
them. The specification of the query structure allowed for actions, which
could be loops containing actions (i.e. The definition of a loop was a
recursive one). I maintained a global pointer to a linked list called
TokenHead, and a local one within various functions, called Head. Here's
the problem: executeNextCommand() calls action(), which in turn calls
forall() (which handles loops) passing it the argument Head. Now, when a
forall loop falls inside another forall loop, forall() calls action() which
in turn calls forall() again, with a different Head value this time. How-
ever, when the second forall() exits, and then the second action() exits,
bringing control back to the first forall(), the value of Head has been
overwritten by the value passed to the sub-call to forall(). Before you
wonder, I've checked, and at no point does the sub-call have any way to
go back up the list and change the values of Head->next. Even more inter-
esting is the fact that the global list, which initially points to the
same place as Head, does not change. Using Turbo Debugger, I have determined
that the value of Head gets changed at the time of the sub-call of forall().
My question: should not the value of Head in the first call be pushed on
the stack or otherwise preserved unchanged when the function is called again?
I have not declared it as static, auto, volatile, const, etc, and when I
do, the program behaves the same. Note that this is not only occurring in
Turbo C, where I did most of the debugging, but also in GNU C on the
class computer. Any ideas: is this supposed to happen, and how can I
get around it? Take your time, the project was due Tuesday anyway.
( Scott Drellishak
(
( "Hey there! Hi there! Ho there! [smack] [smack] [smack]"
( -The Mickey Mouse Club in the Spanish Inquisition
More information about the Comp.lang.c
mailing list