3

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?

Sharat Chandra
  • 4,434
  • 7
  • 49
  • 66
  • See also [this duplicate](http://stackoverflow.com/questions/39206296/how-does-memory-determine-data-type-of-variable) which also attracted some good answers. – Peter Cordes Aug 30 '16 at 01:05

5 Answers5

12

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.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 1
    "If you interpret them as a double, god knows what you'll get." Problems, usually. A `double` tends to be 64 bits. If you interpret them as a `float`, it's a NaN if IEEE754 is used. – Daniel Fischer Mar 18 '12 at 20:02
6

Nowhere, it's just assumed by the code.

heinrich5991
  • 2,694
  • 1
  • 19
  • 26
  • It is important to note that in other languages (C++) there is a thing called dynamic binding with vtables that contain some of the information about the object type. – Mikhail Mar 18 '12 at 19:48
  • 1
    @Misha this question was about C,that's why I haven't added this information – heinrich5991 Mar 18 '12 at 19:49
3

A C application does not store type information. The only enforcement is with how the data is used. And, in C, it is a very simple matter to abuse that enforcement.

Consider the following:

short s;
short *p = &s;
*(long*)p = 0;

This code takes a pointer to a short variable, and then writes to that variable as though it were a long. This overwrites memory beyond what is owned by the variable being used, causing undefined behavior.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
2

This information is not stored. For example, you could do things like

int i=15;
char *p=(char*)&i;
char c=*p; //Now, c contain the first byte of 15, which may be 15 or 0

But don't do this unless you really know what do you do. Do stuff like this uncarefully is a common way to errors.

asaelr
  • 5,438
  • 1
  • 16
  • 22
  • Why would this be wrong ? What would happen if we printf("%c", c); – Sharat Chandra Mar 18 '12 at 19:58
  • @SharatChandra It's fine if you know what you're doing. It's just that if someone doesn't know and does such things, it's usually nonsense. – Daniel Fischer Mar 18 '12 at 20:04
  • @SharatChandra Like Daniel wrote, it's fine when you know what you're doing, but even in this simple example, `c` may be 0 or 15. In more complex cases, we need to be careful also about the alignment. – asaelr Mar 18 '12 at 21:33
1

When the C program is past linking, and don't need to expose any symbols outside, this data does not exist anymore.

The data types are language-related, when the executable is ready, the language don't play a part anymore, as now it's all memory and processor.

MByD
  • 135,866
  • 28
  • 264
  • 277