c++compilationmachine-codeexecutable-format

Are variable identifiers totally needless, at the end of the day?


I've taken a good time studying TOC and Compiler design, not done yet but I feel comfortable with the conceptions. On the other hand I have a very shallow knowledge of assembly and machine code, and I have always the desire/need to connect the two sides( HLL and LLL representation of the code ), as I'm learning C++ with paying great attention to performance and optimization discussions.

C++ is a statically typed language:

My question is: Our variables when written as expressions in the statements of the code, do all these variables ( and other entities with identifiers ) become at runtime, mere instructions of addressing to positions of the virtual memory ( for static and for globals ) and addressing relevant to stack address for local variables?

I mean, after a successful compilation including semantic and syntactic verification, isn't wise to deal with data at runtime as guaranteed entities of target memory bytes without any thinking of any identifier or any checking, with the symbol table no more needed?

If my question appeared to be the type of questions that are due to lacking of learning effort ( which I hope it doesn't ), please just inform me about that, and tell me where to read. If that was the case, then it's honestly because I'm concentrating on C++ nowadays and haven't got the chance yet to have a sound knowledge of low level languages, I apologize for that in advance.


Solution

  • Yes, mostly. There are a few details that will make identifiers remain more than just addresses or stack offsets.

    First we have in RTTI in C++ which means that during runtime the name of at least types may still be available. For example:

    const std::type_info &info = typeid(*ptr_interface);
    std::cout << info.name() << std::endl;
    

    would print the name of whatever type *ptr_interface is of.

    Second, due to the way a program is linked the symbols from the object files may still be present in the executing image. You have for example the linux kernel making use of this as it can produce a backtrace of the stack including the function names. Also it uses knowledge of function names in order to be able to load and link modules. Similar functionality exists in Gnu C library, than when linked for it is able to retrieve function names in stack traces.

    In normal cases though the code will not be affected by the original names of the variables (but the compiler will of course emit code suitable for the type the variable have).