2

Obviously the compiler has to store the information whether variable x is unsigned int or signed int, float or whatever, but where and how?

Is there some sort of lookup table? Where in memory do I find it, in which section of the executable?

defoe
  • 369
  • 2
  • 14
  • 6
    It is called a symbol table. http://stackoverflow.com/questions/69112/what-is-a-symbol-table – erbdex Nov 29 '13 at 06:22
  • 4
    Yes, there is a symbol table contained in the output object files (.o, .obj) that stores the type and name of every variable as well as the return type, calling convention, arguments, etc... for every function in a translation unit (.c or .cpp file, generally). However, this information may be lost **after** linking and generating the executable, in these cases you need a debug symbol table. But you will not find this in memory, usually debug symbols are stored in a separate file and used by the debugger alone; they are not needed for your program to run. – Andon M. Coleman Nov 29 '13 at 06:24
  • Are you just curious how the compilation process work, or is there a larger question underneath this question? If you *magically* had this information, would you use it in your program? How? – chwarr Nov 29 '13 at 06:42

2 Answers2

4

It is implicit, in the instructions that the compiler chose.

For instance, if address 18 contained a float, the compiler may use an instruction to load a floating-point register from address 18. And if it's neighbor at address 20 contained an int, the compiler may load an integer register from the previous address + 2.

As already mentioned, the compiler has a symbol table so it knows where all variables are. This allows it to pick the right instructions. But you can't simply derive the contents of that symbol table from individual instructions.

MSalters
  • 173,980
  • 10
  • 155
  • 350
2

C and C++ implementations usually do not store variable names anywhere, unless you enable debugging information. Variable names are not needed for normal execution, with the exception of exported symbols from shared objects or DLLs.

wilx
  • 17,697
  • 6
  • 59
  • 114
  • But what if you do arithmetic operations, then the CPU must know if it is unsigned int or signed int. – defoe Nov 29 '13 at 07:20
  • 1
    @defoe: Some arithmetic instructions like addition or multiplication are the same at the bit level for signed/unsigned types. For those where signedness matters, there are separate instructions. – Siyuan Ren Nov 29 '13 at 07:24
  • 2
    @defoe The CPU doesn't need to "know" that because the compiler has already emitted the proper instructions for every case. The CPU doesn't do any type analysis. – molbdnilo Nov 29 '13 at 07:33