You are correct in the sense that a stack is used to keep track of the
state of the current 'object' though, actually it is more than an
object. One of its uses is to keep track of the current state of the
system, which may be a single variable or a single object, but can be
as many variables as are necessary to define the current system state.
Basically, a stack is a data structure concept that doesn't restrict
the kind of information in it.
It's very useful in systems programming since one needs a snapshot of
the certain system information (return memory address for example) and
the values of the arguments to a called procedure just before actually
calling it. So, one gathers up all that information, pushes it into a
stack, and calls the routine.
The use of a stack in 'day to day' programming is limited since in most
cases it is easier to use a recursively coded routine. That
effectively shifts the responsibility of managing the stack passes to
the language/compiler. This is especially true when dealing with cases
where the push/pop stack operations occur with some sense of
predictability.
That leaves the stack as a very useful data structure for
'unpredictable' instances where one needs to preserve system state.
And, in the age of event-driven programming, there are opportunities
galore. [And, when one thinks about it, calling a procedure is an
'unpredictable' event.]
One recent instance where a stack was the appropriate data structure
that comes to mind was when I was developing a PowerPoint based add-in
for a client. The add-in would monitor a slide show timing on a slide-
by-slide basis. Part of the requirement was that a slide show could
have one or more slides from which the presenter could branch to a 2nd
show. When that show ended, the presentation would resume from the
slide in the 1st show.
While the client did not expect to ever nest more than one show deep,
the correct data structure for this problem was a stack. And, that
meant I could implement n-deep nesting at no extra cost to me. So,
that's what I did. Every time a 'SlideShowBegin' event happened, my
code took all the variables that defined the system state and stuffed
them into a stack. When a 'SlideShowEnd' event occured, I saved the
information for the current slideshow in a file, and popped the stack.
--
Regards,
Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions