How is the data type and size stored in memory?

assembly, memory-management, types

Solution

As far as the OS is concerned, these are just blocks of memory. It knows nothing about what they contain, other than "random bit strings".

The smarts are all in the compiler - the compiler tracks the types of variables and then generates load and store instructions for the appropriate number of bytes, and generates code that operates on operands of the appropriate size and encoding scheme (e.g., it knows to use an unsigned add operation instead of a signed one on an unsigned int).

Problem

``` double a; unsigned int b; ``` During runtime, how will the OS know how many bytes are affiliated with these variables, and how their bits should be interpreted? If this is language-/OS-dependent, assume C on Windows. Is there a LUT that maps bit-representation of variable identifier to byte-size and data type? From assembly programming on a uC, I recall that the compiler magically knew how many bytes were allocated to a variable, and performed zero-padding/etc appropriately.

Original source