问题描述:

This question has probably been answered before but I have searched these forums and Google but don't think I've asked the correct thing in the most succinct way so I apologise but I'll try to explain my question further:

Say for example you have a variable, let's call it x and it refers to a piece of memory which holds the binary value 01000001. I was wondering: where is the type information for this binary representation held?

Is there like a memory mapping system like a dictionary that maps the binary representation to some predefined list of types, e.g. 0000 to represent an int and 0001 to represent a char, for example?

I feel as though this isn't the case as this would introduce a bulky overhead that would grow as, say for example, more user-defined types were introduced or more variables created. But I imagine this might be the case for weakly-typed languages. (Or maybe not) But what about strongly-typed languages?

网友答案:

I was wondering: where is the type information for this binary representation held?

It's not necessary to store it. Statically typed program compiles to a sequence of instructions targetted at some memory locations. Static typechecking verifies that the instructions operate on the memory locations holding appropriate data. In runtime, however, the instructions can be executed without any verification.

Consider this simple C function:

void increment(int* x) {
    (*x) += 1;
}

At compilation time, the compiler will verify that the value behind the pointer contains a value of type int. The pointer itself is just a number, though, and thus the increment operation can and will be made regardless.

Hence, this program is well-typed, but you lose the aforementioned guarantee.

void increment(void* x) {
    (*(int*)(x)) += 1;
}

The key observation here is that low-level instructions operate on all kinds of data in the same way.

网友答案:

Statically determined types are not known at runtime: they are needed only during compilation, for example to resolve overloading and check that types match the operations you want to run, so the compiler gets rid of them when optimizing.

Some language supports dynamically determined types, such as C++ (only for classes with virtual methods)/java/C#, but also python and javascript. The information of which type those object are at runtime (i.e., the "real" type of the object, not the type of the variable holding it for polymorphism) is used to resolve which method to call at runtime. This information is stored usually as a pointer to a structure defining the type. The runtime overhead of having many types is of a constant quantity of memory for each class. It is usually not important, but there are some exceptions (for example, in Javascript when you write closures you are creating a new type for each object you construct, in some browser this is pretty expensive). You may have an heavy overhead during compilation instead: this happens a lot in C++ templates for example.

相关阅读:
Top