Relocatable code and Symbol Table

This is a discussion on Relocatable code and Symbol Table within the C Programming forums, part of the General Programming Boards category; Can anyone please tell me
1)the purpose of symbol table
2)heap and stack, and whether global variables are allocated memory ...

Stack is memory for temporary storage
all stack members are with equal size
it operates in LIFO fashion (L)ast (I)n (F)irst (O)ut
this means that the last thing you inserted into the stack will be the first to get .. theese two operations :
the inserting and getting are usualy called
push and pop
the stack is also used extensivly when function call is made

the local variables are push-ed onto the stack
but the globals are part of the heap which is created in the begining

so how do the function call is made:

some_code....
....
int var = 10;///here a local variable is created onto the stack
func1(var);/*here the var is push-ed into the stack so the function 'func1' could use it*/

/*here function 'func1' recieves the var because it was the last one push-ed into the stack*/
void func1(int v) {
return 10;/*...and func returns the value 10 into a special proccesor register called 'eax' or 'ax' for 16 bit computers*/
}

You require a more specific explanation so that learning occurs. Here, I will explain these things in detail:

1)the purpose of symbol table
---------------------------------------
When a compiler walks through your sourcefiles and tries to compile your code into objectcode, it has to first identify where things are and what they uniquely are so that it can reference things and locations.

Remember, you are submitting a textfile to your compiler. Just text. It doesn't know what you are trying to do, it doesn't know anything about logic.

So, on it's pass(es), it will first identify each unique variable, variable type, function/procedure name, constant, and enum. It places these in a tree structure called a symbol table. That way, if it sees one of these elements that it has already seen, it references the one it in the tree. If not, it adds it to the tree.

By doing it this way, it can tell whether or not it has seen a variable declared and defined before, or not.

If it finds a symbol that has been used (referenced), but it hasn't been declared (defined) before, you get an 'undefined' error. A linker error.

So, a symbol table is a way the compiler keeps track of and organizes all your 'symbols' and labels, prior to linking everything together.

2)heap and stack, and whether global variables are allocated memory from heap or stack
-------------------------------------------------------------------------------

The 'heap' and 'stack' are two different things. All the RAM that your computer has can be considered one heap. Usually this entire Heap 'O RAM is allocated to the operating system. Such as Windows or Mac O/S. When the O/S loads and runs, one of the things it does is load a module called a Memory Manager. This memory manager then takes all the 'unused' heapspace and divides it up into chunks for applications and unused space. If an application is run, the memory manager parcels out a chunk of the O/S heap's unused RAM to the application as it's heap. The application, likewise, relies on the memory manager to help it keep its heap (or subheap) organized, into code space, data space, unused space, and stack space.

Okay, now that last is important. stack space. Usually, a heapspace (or just 'heap') is organized such that the jumptable is down low, the code comes next, and the constants and variables come next. This space is followed by unused space, and then at the very top of the heap, is the stack chain.

The stack chain (or simply 'stack' as it is frequently called) is nothing more than a consecutive chunk of RAM that shrinks and grows in length, as needed. It is usually made up of words (32-bits wide), but that can change based on what else is done with the stack (more on that in a moment).

Initially, however, we'll just say it is a list of consecutive unsigned long words. Everytime the CPU runs the code of your application and gets told it has to branch somewhere (like to run a function or a procedure), it takes the value out of a register called a PC or 'Program Counter' and 'pushes' it onto the stack. This is called a 'return address'. This causes the stack to grow down into the unused portion of the heap, _towards_ the code and data section.

Usually a stack is expected to take up no more than about 32K of your heap.

As the function or procedure returns, the last address on the stack is 'popped' off the stack and pushed back into the PC. This tells the CPU where to begin executing so it can run, just as though it had never gone elsewhere to run a function.

Now, as for variable allocation-- all variables are allocated from the heap. Your entire program resides in its own heap.

Global variabes, within the heap, reside in a preallocated chunk of RAM (allocated a compile time) called the 'Data space'. Usually following the code space. Local variables however, are allocated 'on the fly' on an as-needed basis. If a function gets called, not only is the return address pushed onto the stack, but enough additional bytes are pushed onto the stack, to hold any variables allocated by the function/procedure. When the function exits, the space on the stack is disposed of (reclaimed) and so the stack again shrinks.

3)what do we mean by relocatable code
------------------------------------------------------

Sometimes code is written such that it actually references physical locations in RAM. Such code is not relocatable, because it has to be in a specific place in order to work.

To make code 'relocatable', it must reference everything relative to itself. That way, whereever it is placed in RAM, the references still work.