ctypesruntime-type

Identifying data type of a value in memory in C?


How does a program/application know that the data in a memory address is of a certain data type.

For instance, suppose there is int a; and suppose variable a is stored in address 0x100. Where is the the information stored that says it is of type int?


Solution

  • In languages like C the information is always "stored" in the way you interpret the data. Some niceness is added by the compiler which to some extent understands the types of your variables and tries to prevent operations which don't make sense.

    For instance, suppose you have the bits: 0xFFFFFFFF. If you interpret them as "a 32b unsigned int" you'll get 4294967295. If you interpret them as "a 32b signed int" you'll get -1(*). If you interpret them as a double, god knows what you'll get.