Expert C Programming

(Jeff_L) #1

What the C Runtime Does with Your a.out


Now we come to the fine detail of how C organizes the data structures of a running program. There
are a number of runtime data structures: the stack, activation records, data, heap, and so on. We will
look at each of these in turn, and analyze the C features that they support.


The Stack Segment


The stack segment contains a single data structure, the stack. A classic computer science object, the
stack is a dynamic area of memory that implements a last-in-first-out queue, somewhat similar to a
stack of plates in a cafeteria. The classic definition of a stack says that it can have any number of
plates on it, but the only valid operations are to add or remove a plate from the top of the stack. That is,
values can be pushed onto the stack, and retrieved by popping them off. A push operation makes the
stack grow larger, and a pop removes a value from it.


Compiler-writers take a slightly more flexible approach. We add or delete plates only from the top,
but we can also change values that are on a plate in the middle of the stack. A function can access

Free download pdf