Address of register variable

后端 未结 8 2081
灰色年华
灰色年华 2020-11-29 09:34

In C, we cannot use & to find out the address of a register variable but in C++ we can do the same. Why is it legal in C++ but not in C? Can someone please explain this

8条回答
  •  一生所求
    2020-11-29 10:07

    Sorry about the super late answer.

    The problem is that, in C, register originally meant storing values in a register which is why only int and char can be used for it. But with time and especially standard C++, it broadened to "fast access" rather than "in register of CPU". So in C++, an array maybe a register type but we know that it is not possible to store arrays in a CPU register. Hence, it is logically okay to address a C++ register (in the above sense), but will still make no sense if the values are actually in a CPU register.

提交回复
热议问题