How does memory determine data type of variable?
This question already has an answer here:
There is no need for the memory to know what type of data is being stored and where. Memory is just about storing values in blocks with addresses.
Its the compilers job to determine the type and fetch the appropriate data from memory.
suppose i have this value stored in my memory how will memory determine this is string or int
It doesn't.
How datatype are stored in memory
If type information is stored and how is completely up to the programming language and runtime environment used. All implementations of C (compiler and standard library) I know of do not store the data type alongside the values. Other programming languages and rumtimes do.
But with C how contents of memory are interpreted is determined entirely by the program, ie the machine level operations performed on the contents of a particular memory location. And it is up to the programmer (you) not to lie to the compiler about what can be found in memory locations.
bits is bits. Not only does it not have a specific type as the memory is concerned it can have many types all at the same time. The notion of types is mostly only relevant to the human, and at times to the logic, but never to the memory.
You might have a program where you have a variable that is an address, the base address for a structure lets say. But when you want to access an item in that structure, some bits are collected from memory that you the human recognize as address bits, but then they go into logic that does addition so that the offset into what you consider to be a structure can be computed. That adder only sees those bits as operands, neither signed nor unsigned as an adder doesnt know the difference thanks to twos complement, the addition is performed and in your human mind those bits are an address, but to the logic they are just bits, maybe landing in a register or maybe just one step in a register+offset load or store instruction. those bits might need to go through an mmu to be converted from virtual to physical. Those bits are not just an offset in a table, more math, operands into an adder, then some of the bits get replaced to make them a physical address, most of your bits you considered an address are now gone, replaced.
In grade school I had a pencil, many actually, but on one particular day one could imagine one pencil. On the bus ride to school it might have been a pain my leg in my pocket sitting down. Maybe that same pencil then became a back scratcher. Or perhaps something to gnaw on like a dog bone. Then eventually it may have been used to write down spelling words, an english pencil. Then in math class it was used to add numbers, an addition pencil. Art class to do art, an art pencil. Science. History, etc. Just like bits in memory, general purpose, only the context in which they are used for one clock cycle defines them by logic as something else, and then they are just bits again.
链接地址: http://www.djcxy.com/p/82490.html上一篇: C / C ++中的静态作用域
下一篇: 内存如何确定变量的数据类型?