Dereferencing a 50% out of bound pointer (array of array)

后端 未结 2 1786
有刺的猬
有刺的猬 2020-12-07 00:06

This is a new question in my \"I don\'t understand pointers in C and C++\" collection.

If I mix the bits of two pointers with equal values (pointing to the same mem

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 00:42

    What I really want to understand is: what does "p points to object x" means.

    The object p contains a value that corresponds to the location of the object x in memory.

    That's it. That's all it means. You seem determined to make this more complicated than it needs to be.

    Pointer types are not arithmetic types, and aren't meant to be arbitrarily munged like that. Valid pointer values are obtained by using the unary & operator on an lvalue, using an array expression that isn't the operand of the sizeof or unary & operator, or calling a library function that returns a pointer value.

    Everything beyond that (size, representation, physical vs. virtual, etc.) is an implementation detail, and implementations vary widely when it comes to representing addresses. That's why the standards don't say anything about what to expect when you play Dr. Frankenstein with pointer values.

    If you are intimately familiar with your platform's addressing conventions (both virtual and physical), and you know how your implementation lays out items in memory and how it represents pointer types, and you have a valid use case for hacking your pointer values this way, then hack away to your heart's content - neither language standard has anything to say on the subject.

提交回复
热议问题