How does memory determine the data type of a variable?

enter image description here

Suppose I have this value stored in my memory, how does memory define it as a string or int. How data type is stored in memory

+1


source to share


4 answers


In memory, there is no need to know what type of data is stored and where. Memory is simply storing values ​​in blocks of addresses.



Compilers property for determining the type and fetching the appropriate data from memory.

+2


source


Suppose I have this value stored in my memory, how does memory define it as a string or int

This is not true.

How data type is stored in memory



If the type information is stored and how the programming language and the runtime are entirely dependent. All C implementations (compiler and standard library) that I know do not preserve data type along with values. Other programming languages ​​and frequency.

But with C, how the contents of memory are interpreted is entirely programmed, that is, machine-level operations performed on the contents of a particular memory location. And the programmer (you) shouldn't lie to the compiler about what can be found in memory locations.

+9


source


bit - bit. Not only does it not have a specific type, like memory, it can have many types at the same time. The notion of types mostly refers only to human and sometimes to logic, but never refers to memory.

Perhaps you have a program where you have a variable that is an address, such as the base address for a struct. But when you want to access an element in that structure, some of the bits are collected from memory, which you human recognize as address bits, but then they go into the logic that does the addition, so the offset to what you think of the structure can be calculated. This adder only sees these bits as operands, neither signed nor unsigned, since the adder does not know the difference thanks to two's complement, the addition is done and in your human mind these bits are an address, but logic they are just bits, maybe a register fit or, maybe only one step in register + offset load or instruction storage. these bits maybemust go through mmu to convert from virtual to physical. These bits are not just an offset in the table, more math, the operands to the adder, then some of the bits are replaced to make them a physical address, most of your bits that you thought were an address are now gone, replaced.

In the classroom I had a pencil, many actually, but on one particular day one could imagine one pencil. On the bus ride to school, there may have been pain when my leg was sitting in my pocket. Perhaps the same pencil became the back scraper. Or, perhaps, gnaw something like dog bones. Then, after all, it may have been used to write spelling words, an English pencil. Then in math class, it was used to add numbers, extra pencil. Art class for art, art pencil. The science. History, etc. Like bits in memory, common targets, only the context in which they are used for one clock defines them logically as something else, and then they are bits again.

+2


source


As already mentioned: this is not the case. This task is compiler and programmer dependent. On the one hand, this can lead to programming errors, but on the other hand, it allows you to do some tricks like:

//Change a lowercase Character to uppercase character:
char x = 'a';
printf("%c\n", x);
x += 32;
printf("%c\n", x);

      

+1


source







All Articles