What data structure does the C runtime environment use to store information about a variable such as font size, etc.?

what data structure is used at run time c to store information about a variable of type, size, etc.

Example:

void foo(){ int bar=0, goo=44; int*q, *p = &goo; //some code follows bar = goo + bar*9; ... q=p; ... } 

In the above code, we have a local variable bar and goo that will be allocated on the stack when control reaches the function foo. But how will runtime be determined at a later point, when these variables refer that these variables are of such and such a type and so and so size?

+4
source share
4 answers

Runtime does not store any such information - it compiles into binary code, which the compiler generates as constants. The compiler knows the size of each type, so it knows how to create the right machine code to clear the stack, access array elements, access structure fields, etc. There is no need to store this information at runtime because the binary already contains all the relevant instructions.

+2
source

The values โ€‹โ€‹of the variables are known at compile time, so there is no need to store them at run time.

 int bar = 0; 

just translates to

 "shift the stack pointer by 4 bytes" 

Variable types do not have to be known at all at runtime. You may get compiler warnings about incompatible types, for example, printing an int with %c , but for you this is more of a consistency check. A variable simply names a piece of data, and you decide how to interpret it - as an integer, as a pointer, as 4 characters ...

+2
source

Typically, such code is hardcoded in the base code at compile time, and such data is not stored at run time.

As an example, suppose a variable string is pushed onto the stack. The compiler will remember where it was placed on the stack, and all subsequent references to the bar will become machine code, which captures the size of the variable at the corresponding stack offset. The same is true for variables stored in processor registers; references to them are converted to machine code, which refers to the corresponding register and byte length.

+2
source

The compiler knows this information for the configured platform. The compiler also has C include header files to tell it the size of various data types. See this

0
source

Source: https://habr.com/ru/post/1414406/


All Articles