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 ...

  1. #1
    Unregistered
    Guest

    Question Relocatable code and Symbol Table

    Can anyone please tell me
    1)the purpose of symbol table
    2)heap and stack, and whether global variables are allocated memory from heap or stack
    3)what do we mean by relocatable code

  2. #2
    Unregistered
    Guest
    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*/
    }

  3. #3
    *
    Guest
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. VC++ a POS
    By Welder in forum Windows Programming
    Replies: 40
    Last Post: 11-07-2007, 03:07 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21