11

Just came across the register keyword in C++ and I wondered as this seems a good idea (keeping certain variables in a register) surely the compiler does this by default?

So I wondered is this keyword still used?

user997112
  • 29,025
  • 43
  • 182
  • 361
  • 1
    But `inline` is not only a hint. –  May 20 '12 at 16:16
  • http://stackoverflow.com/questions/1443982/when-do-compilers-inline-c-code "note: inline is only hint to the compiler. The compiler is not required to inline it. – Loki Astari" – user997112 May 20 '12 at 16:17
  • 4
    @user997112: That's taken out of context. `inline` *does* have other, real implications. – Kerrek SB May 20 '12 at 16:18
  • 2
    "Is it still used" is something else entirely than "should you still use it". – harold May 20 '12 at 16:30
  • @user997112: Better explanation of 'inline' if you are going to qote me: http://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method – Martin York May 20 '12 at 18:18

6 Answers6

13

Most implementations just ignore the register keyword (unless it imposes a syntactical or semantical error).

The standard also doesn't say that anything must be kept in a register; merely that it's a hint to the implementation that the variable is going to be used very often. Its use is even deprecated.

7.1.1 Storage class specifiers [dcl.stc]

3) A register specifier is a hint to the implementation that the variable so declared will be heavily used. [ Note: The hint can be ignored and in most implementations it will be ignored if the address of the variable is taken. This use is deprecated (see D.2). — end note ]

7

The standard says this (7.1.1(2-3)):

The register specifier shall be applied only to names of variables declared in a block (6.3) or to function parameters (8.4). It specifies that the named variable has automatic storage duration (3.7.3). A variable declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default.

A register specifier is a hint to the implementation that the variable so declared will be heavily used. [ Note: The hint can be ignored and in most implementations it will be ignored if the address of the variable is taken. This use is deprecated (see D.2). — end note ]

In summary: register is useless, vestigial, atavistic and deprecated. Its main purpose is to make the life of people harder who are trying to implement self-registering classes and want to name the main function register(T *).

akappa
  • 10,220
  • 3
  • 39
  • 56
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
6

Probably the only remotely serious use for the register keyword left is a GCC extension that allows you to use a hard-coded hardware register without inline assembly:

register int* foo asm("a5");

This will mean that any access to foo will affect the CPU register a5.

This extension of course has little use outside of very low-level code.

  • Since it's an extension, it's still supported regardless of `register int* foo` (without `asm`) being supported in the version of C++ you're compiling for. Also, these days that extension only means that `"r"(foo)` will pick that register when used as a constraint for an `asm` statement. It's *not* guaranteed to do anything else at any other time, although GCC still does generally use extra instructions to copy a local to that register when it's declared this way. – Peter Cordes Nov 12 '22 at 04:27
5

Only specific number of registers are available for any C++ program.

Also, it is just a suggestion for the compiler mostly compilers can do this optimization themselves so there is not really much use of using register keyword and so more because compilers may or may not follow the suggestion.

So the only thing register keyword does with modern compilers is prevent you from using & to take the address of the variable.

Using the register keyword just prevents you from taking the address of the variable in C, while in C++ taking the address of the variable just makes the compiler ignore the register keyword.

Bottomline is, Just don't use it!

Nicely explained by Herb:
Keywords That Aren't (or, Comments by Another Name)

Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

No, it's not used. It's only a hint, and a very weak one at that. Compilers have register allocators, they can figure out which variables should be kept in registers (and account for things you probably never thought about).

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • Actually it *is* used, just look at why Termux doesn't have openjdk: clang doesn't support it anymore and OpenJDK mobile/jdk9 uses it. – kb1000 Dec 20 '18 at 10:52
0

The keyword "register" has been deprecated since the 2011 C++ standard; see "Remove Deprecated Use of the register Keyword". It should therefore not be used.

In my own experiments I found that debug code generated by gcc (v8.1.1) does differ if the "register" keyword is used; the generated assembly code allocates designated variables to registers. Benchmarks even showed that this code ran faster (than code without "register"). This is irrelevant, however, as release (optimised) code showed no differences (ie, using "register" had no effect). Vacbob states here that if any optimization is enabled, then gcc ignores "register". My own tests confirm this.

So, in summary, don't use "register" and if debug code appears to run faster when "register" is used, bear in mind that the optimized release code will not.