Are pointer variables just integers with some operators or are they “symbolic”?

后端 未结 4 1573
长情又很酷
长情又很酷 2020-11-28 15:40

EDIT: The original word choice was confusing. The term \"symbolic\" is much better than the original (\"mystical\").

In the discussion about my previous C++ questio

4条回答
  •  误落风尘
    2020-11-28 16:10

    If you turn off the optimiser the code works as expected.

    By using pointer arithmetic that is undefined you are fooling the optimiser. The optimiser has figured out that there is no code writing to b, so it can safely store it in a register. As it turns out, you have acquired the address of b in a non-standard way and modify the value in a way the optimiser doesn't see.

    If you read the C standard, it says that pointers may be mystical. gcc pointers are not mystical. They are stored in ordinary memory and consist of the same type of bytes that make up all other data types. The behaviour you encountered is due to your code not respecting the limitations stated for the optimiser level you have chosen.

    Edit:

    The revised code is still UB. The standard doesn't allow referencing a[1] even if the pointer value happens to be identical to another pointer value. So the optimiser is allowed to store the value of b in a register.

提交回复
热议问题