Sunday, March 22, 2015

staic and global variable on same data segment but how do they differentiated


This question either came into your mind or asked in the interview:

Answer is:

These variables are called "symbols", and during compiling a table is generated, the "symbol table". This table contains the name, type, scope and memory pointer to each symbol (this is like the minimum, you usually have a bunch of more stuff), and each time a reference is made to an symbol in a specific scope, it's substituted for an index into the table. These indices are unique, so is the combination of name+scope.
So in short, the names of the variables are simply decoration, internally the compiler works with a symbol table and indices into it. Statics are initialized during program startup by iterating through a table of pointers to them and putting the correct values in place.




Reference:

http://stackoverflow.com/questions/3474611/where-and-when-do-the-global-static-and-local-static-get-stored-and-initialized

1 comment:

  1. http://www.quora.com/How-do-I-compile-an-OS-kernel

    http://www.quora.com/I-want-to-build-my-own-OS-from-scratch-How-do-I-begin

    ReplyDelete