C pointers and the physical address

前端 未结 11 749
傲寒
傲寒 2020-12-03 15:43

I\'m just starting C. I have read about pointers in various books/tutorials and I understand the basics. But one thing I haven\'t seen explained is what are the numbers.

相关标签:
11条回答
  • 2020-12-03 16:41

    That's a virtual address of anumber variable. Every program has its own memory space and that memory space is mapped to the physical memory. The mapping id done by the processor and the service data used for that is maintained by the operating system. So your program never knows where it is in the physical memory.

    0 讨论(0)
  • 2020-12-03 16:45

    Lots of PC programmer replies as always. Here is a reply from a generic programming point-of-view.

    You will be quite interested in the actual numerical value of the address when doing any form of hardware-related programming. For example, you can access hardware registers in a computer in the following way:

    #define MY_REGISTER (*(volatile unsigned char*)0x1234)
    

    This code assumes you know that there is a specific hardware register located at address 0x1234. All addresses in a computer are by tradition/for convenience expressed in hexadecimal format.

    In this example, the address is 16 bits long, meaning that the address bus on the computer used is 16-bits wide. Every memory cell in your computer has an address. So on a 16-bit address bus you could have a maximum of 2^16 = 65536 addressable memory cells. On a PC for example, the address would typically be 32 bits long, giving you 4.29 billion addressable memory cells, ie 4.29 Gigabyte.

    To explain that macro in detail:

    • 0x1234 is the address of the register / memory location.
    • We need to access this memory location through a pointer, so therefore we typecast the integer constant 0x1234 into an unsigned char pointer = a pointer to a byte.
    • This assumes that the register we are interested in is 1 byte large. Had it been two bytes large, we would perhaps have used unsigned short instead.
    • Hardware registers may update themselves at any time (their contents are "volatile"), so the program can't be allowed to make any assumptions/optimizations of what's stored inside them. The program has to read the value from the register at every single time the register is used in the code. To enforce this behavior, we use the volatile keyword.
    • Finally, we want to access the register just as if it was a plain variable. Therefore the * is added, to take the contents of the pointer.

    Now the specific memory location can be accessed by the program:

    MY_REGISTER = 1;
    unsigned char var = MY_REGISTER;
    

    For example, code like this is used everywhere in embedded applications.

    (But as already mentioned in other replies, you can't do things like this in modern PCs, since they are using something called virtual addressing, giving you a slap on the fingers should you attempt it.)

    0 讨论(0)
  • 2020-12-03 16:45

    apointer is the "address" of the variable anumber. In theory, it could be the actual physical place in RAM where the value of anumber is stored, but in reality (on most OS'es) it's likely to be a place in virtual memory. The result is the same though.

    0 讨论(0)
  • 2020-12-03 16:47

    It's the address or location of the memory to which the pointer refers. However, it's best if you regard this as an opaque quantity - you are never interested in the actual value of the pointer, only that to which it refers.

    How the address then relates to physical memory is a service that the system provides and actually varies across systems.

    0 讨论(0)
  • 2020-12-03 16:47

    It's the address of the pointer.

    "anumber" takes up some space in RAM, the data at this spot contains the number 10.

    "apointer" also takes up some space in RAM, the data at this spot contains the location of "anumber" in RAM.

    So, say you have 32 bytes of ram, addresses 0..31

    At e.g. position 16 you have 4 bytes, the "anumber" value 10

    At e.g. position 20 you have 4 bytes, the "apointer" value 16, "anumber"'s position in RAM.

    What you print is 20, apointer's position in RAM.

    Note that this isn't really directly in RAM, it's in virtual address space which is mapped to RAM. For the purpose of understanding pointers you can completely ignore virtual address space.

    0 讨论(0)
提交回复
热议问题