How are static variables with the same name in different functions identified by the System?

AFAIK, we can have two static variables with the same name in different functions? How are these managed by the compiler and symbol table? How are their identities managed seperately?


Solution 1:

Compilers don't store static variables' names in the linking symbol table. They are just some memory that is part of the module as far as the linker is concerned. (this may not be 100% true in all cases but it is effectively true)

The names of static variables are usually included within the debugging symbol table.

When you feed a .c file to the compiler it keeps up with the names of all known symbols so that it can recognize them for what they are when they come up in future code. It also remembers them so that it can give useful error/warning messages, but it pretty much forgets about them when generating output files (unless debugging symbols are being generated).

Solution 2:

They are likely mangled in the table, in a similar way to how overloaded functions are implemented.

See dumpbin /symbols foo.obj if you want to peek at the table, or use objdump on linux.