C pointers and the physical address

前端 未结 11 748
傲寒
傲寒 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:21

    it is not the address of the variable anumber that is printed but it is the address of the pointer which gets printed.look carefully.had it been just "apointer",then we would have seen the address of the anumber variable.

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

    Yes, exactly that - it's the address of the apointer data in memory. Local variable such as anumber and apointer will be allocated in your program's stack, so it will refer to an address in the main() function's frame in the stack.

    If you had allocated the memory with malloc() instead it would refer to a position in your program's heap space. If it was a fixed string it may refer to a location in your program's data or rodata (read-only data) segments instead.

    0 讨论(0)
  • It's the address of the memory1 location where your variable is stored. You shouldn't care about the exact value, you should just know that different variables have different addresses, that "contiguous memory" (e.g. arrays) has contiguous addresses, ...

    By the way, to print the address stored in a pointer you should use the %p specifier in printf.


    1. Notice that I did not say "RAM", because in most modern OSes the "memory" your process sees is virtual memory, i.e. an abstraction of the actual RAM managed by the OS.
    0 讨论(0)
  • 2020-12-03 16:37

    A lot of people told you, that the numeric value of a pointer will designate its address. This is one way how implementations can do it, but it is very important, what the C standard has to say about pointers:

    • The nil pointer has always numeric value 0 when operated on in the C programming language. However the actual memory containing the pointer may have any value, as long as this special, architecture dependent value is consistently treated nil, and the implementation takes care that this value is seen as 0 by C source code. This is important to know, since 0 pointers may appear as a different value on certain architectures when inspected with a low level memory debugger.
    • There's no requirement whatsoever that the values of the pointer are in any way related to actual addresses. They may be as well abstract identifiers, resolved by a LUT or similar.
    • If a pointer addresses an array, the rules of pointer arithmetic must hold, i.e. int array[128]; int a, b; a = (int)&array[120]; b = (int)&array[100]; a - b == 20 ; array + (a-b) == &array[20]; &array[120] == (int*)a
    • Pointer arithmetic between pointers to different objects is undefined and causes undefined behaviour.
    • The mapping pointer to integer must be reversible, i.e. if a number corresponds to a valid pointer, the conversion to this pointer must be valid. However (pointer) arithmetic on the numerical representation of pointers to different objects is undefined.
    0 讨论(0)
  • 2020-12-03 16:38

    It's a memory address, most likely to the current location in your program's stack. Contrary to David's comment, there are times when you'll calculate pointer offsets, but this is only if you have some kind of array that you are processing.

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

    in this case &apointer represent the address in RAM memory of the pointer variable apointer

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