How does a program manage type information?

I'm just wondering how type information is managed internally by a C ++ program?

eg. float f; 32 bits of memory are required. But since all the bits are used to store the value, how does the program remember it as a float type?

(I know this is a pretty null question ...)

+6
c ++ types
source share
5 answers

You need to distinguish between the static type (compilation time) and the dynamic (run-time) type.

Static types are controlled by the compiler at compile time and, with the exception of type-specific semantics embedded in the resulting code, are forgotten at runtime.

Dynamic type information is usually stored in so-called "virtual tables" of the type, which contain pointers to all virtual functions that the type has, and what little information such as the runtime supported by C ++. Only polymorphic types (those that have at least one virtual function) will have attached information about the dynamic type.

+12
source share

Usually (i.e. if you are not using RTTI), the program does not know this kind of thing (although the compiler is very strict to check it to avoid errors). The program code simply executes "as if", that is, if you wrote code that uses the float variable, then it will execute floating point instructions for any value that it loads from the corresponding memory address.

If you use RTTI (runtime type information), then the compiler stores a lot of additional information in the executable file, which also allows you to determine the type at runtime.

Finally, with virtual inheritance, vtable stores type information (although not directly accessible). For each type of object (not for each object, but for the type, that is, for all objects), the compiler generates a special "transition table" that allows you to correctly display overloaded functions. You don’t know about it, and you don’t see anything from him, but he "will just work." However, this is some type of information, implicitly.

+7
source share

C ++ usually does not need type information, since it is a static language - this means that all type checking is performed at compile time, and not at run time.

Let's say you have this code:

float fn(float a) { return a + 1.f; } 

... the compiler already knows that fn should get a float, add 1.f to it and return a float. It can generate machine code accordingly and there is no need to search at runtime.

+4
source share

The compiler manages types. The executable executable does not work. For an executable, these 4 bytes in memory are just 4 bytes in memory. But the compiler ensured that only whole instructions or only floating point instructions worked on them.

+3
source share

It is probably useful to note that C ++, after compilation, is the assembly language for any cpu command installed to target the compiler.

A simple thing, such as float i = 2.0 + n; "turns into" set aside 32 bits, take 2.0 and this other group of 32 bits, and execute a 32-bit version of FLOATING POINT ADD. Then ASSIGN, so we just reserved 32 bits "

0
source share

All Articles