4

Cited from the book "Thinking in c++" the section about register variable: "There are restrictions to the use of register variables. You cannot take or compute the address of a register variable. A register variable can only be declared within a block (you cannot have global or static register variables)."

So I wrote this piece of program to test:

int global = 2;
// error
// register int global2 = 3;

int main() {
    register int local2 = 2;
    cout << local2 << " " << &local2 << endl;
}

However g++ generates no error and the address of local2 is printed out. So why I can take the address with no error?

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
darklord
  • 5,077
  • 12
  • 40
  • 65
  • 1
    It's just a compiler hint - the treatment of the variable does not change at the language level. – Xeren Narcy Jul 04 '16 at 02:00
  • 2
    `register` acts as a hint only. If you take the address of the variable, it will get an address regardless of whether you used `register`. – Dmitri Jul 04 '16 at 02:01
  • Possible duplicate of [Register keyword in C++](http://stackoverflow.com/questions/3207018/register-keyword-in-c) – deW1 Jul 04 '16 at 02:01
  • 5
    `register` is deprecated and I think it may be removed in `C++17` – Galik Jul 04 '16 at 02:05
  • 3
    You incorrectly tagged the question both `c` and `c++`; they are very different languages. Indeed it would be a constraint violation (mandatory diagnostic) in C, but your program is nothing like valid C anyway. It was also a constraint violation in earlier versions of C++ (you might be able to get a compiler error by playing with `-std=c++??` options) but apparently now `register` is completely ignored in C++. – R.. GitHub STOP HELPING ICE Jul 04 '16 at 02:29
  • You are usually better off letting the optimizer in your complier figure out how to use registers for you. I'd forgotten this keyword even exits. It is not particularly useful anymore. I would not be surprised if it is outright ignored by most compilers. – tukra Jul 04 '16 at 05:01
  • 1
    @Galik [yes it is removed in C++17](http://stackoverflow.com/a/30809775/1708801) – Shafik Yaghmour Jul 04 '16 at 05:40

2 Answers2

6

It is true that, historically, you could not take the address of a register variable.

This is now just a historical footnote. The current C++ standard states that the register keyword

... specifies that the named variable has automatic storage duration (3.7.3).

In other words, it means ...nothing much. The restriction that an address cannot be taken of a register variable is no longer a part of the current C++ standard.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1

g++ is choosing not to follow your register declaration. It does not have to, and in this case may either be choosing to ignoring it, or smart enough to turn it off because you take the address of the variable.

Long ago we tried to write assembly language in C, and we knew far too much about how many registers our processors had, and thought we were smarter than the compilers of that age at allocating CPU resources.

We have given up that notion, as processors have become far more complex, the compilers have become far more advanced (on a modern intel processor the load instruction itself is turing complete), and our wetware has remained essentially the same.

woolstar
  • 5,063
  • 20
  • 31