How are names remembered with the corresponding memory address?

To define my surprise, I think it would be great with an example:

  • The variable x is declared with an initial value of 2 and has a global scope

    int x = 2;
    
          

  • Since this variable x has global scope, it is visible everywhere, which means that the compiler must somehow remember its memory address. In any case, x is then changed from a given function called "bla"

    void bla()
    {
        x = 5;
    }
    
          

  • So the compiler (not sure if it can handle all of this) remembers this name "x" and can somehow find out the memory address belonging to the name x ...

So my questions are:

  • How does the compiler (still not sure about this) remember which names / ids and memory address belong to each other? and where is this information stored - is it also stored as memory addresses?
  • Technically, it would be faster if the programmer wrote the actual memory addresses instead of names so that the compiler (you probably already know that I'm not sure what handles these things) doesn't have to replace all the variables, etc. with addresses?

Thank! I really tried not to do anything ambiguous, but if you still think this is confusing, please comment or suggest an edit + please correct me if I'm wrong, thanks again!

+3


source to share


1 answer


For a deeper understanding, you need to consult some of the learning resources about the language and the compiler.

The C ++ language definition contains the concept of declaration points and scopes. These things must be respected by compilers, so for every expression in well-formed code, the compiler will know what scope the expression belongs to and how nested scopes are.

A typical compiler implementation will have a data structure where each region is associated with a symbol table that lists the declared / defined symbols of the current region. Now, when the compiler (a typical implementation could possibly be completely different!) Encounters a symbol in the code (for example x = 5;

), it will look into the symbol table of the current region and its parent region until the symbol x

. The use of the symbol is then linked to the definition of the symbol as it was found in one of the symbol tables.

Skipping some details, your example contains at least the following nesting area:

global scope
  function bla scope
    function bla body scope

      



Since it is x

not declared locally / not defined in any internal scope, the compiler will associate usage with a definition that is in the global scope.

Technically, it would be faster if the programmer wrote the actual memory addresses instead of names so that the compiler (you probably know I'm not sure I can handle these things) doesn't have to replace all the variables, etc. addresses?

When we talk about development, the compiler will be much faster than the programmer in this task, so there is no need to save time.

As far as program execution is concerned, the symbol no longer exists (except for debug information) because the compiler replaces it at its local memory address. Generally, you cannot save runtime by doing any thing manually that the compiler would otherwise do.

+1


source







All Articles