Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a C++ variable in a register

I would like some clarification regarding a point about the storage of register variables: Is there a way to ensure that if we have declared a register variable in our code, that it will ONLY be stored in a register?

#include<iostream>

using namespace std;

int main()
{
    register int i = 10;// how can we ensure this will store in register only.
    i++;
    cout << i << endl;
    return 0;
}
like image 303
ashutosh kumar Avatar asked Apr 09 '13 17:04

ashutosh kumar


People also ask

Where are register variables stored C?

Register variable values are stored in CPU registers, rather than in memory where, normal variables are stored. Registers are temporary storage units in CPU.

How do you declare a register variable?

“register” keyword is used to declare the register variables. Scope − They are local to the function. Default value − Default initialized value is the garbage value. Lifetime − Till the end of the execution of the block in which it is defined.

Which datatype can be stored in register?

Correct Option: Dfloat, long and int data type can be stored in register.


2 Answers

You can't. It is only a hint to the compiler that suggests that the variable is heavily used. Here's the C99 wording:

A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.

And here's the C++11 wording:

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 fact, the register storage class specifier is deprecated in C++11 (Annex D.2):

The use of the register keyword as a storage-class-specifier (7.1.1) is deprecated.

Note that you cannot take the address of a register variable in C because registers do not have an address. This restriction is removed in C++ and taking the address is pretty much guaranteed to ensure the variable won't end up in a register.

Many modern compilers simply ignore the register keyword in C++ (unless it is used in an invalid way, of course). They are simply much better at optimizing than they were when the register keyword was useful. I'd expect compilers for niche target platforms to treat it more seriously.

like image 168
Joseph Mansfield Avatar answered Sep 28 '22 04:09

Joseph Mansfield


The register keyword has different meanings in C and C++. In C++ it is in fact redundant and seems even to be deprecated nowadays.

In C it is different. First don't take the name of the keyword literally, it is has not always to do with a "hardware register" on a modern CPU. The restriction that is imposed on register variables is that you can't take their address, the & operation is not allowed. This allows you to mark a variable for optimization and ensure that the compiler will shout at you if you try to take its address. In particular a register variable that is also const qualified can never alias, so it is a good candidate for optimization.

Using register as in C systematically forces you to think of every place where you take the address of a variable. This is probably nothing you would want to do in C++, which heavily relies on references to objects and things like that. This might be a reason why C++ didn't copy this property of register variables from C.

like image 36
Jens Gustedt Avatar answered Sep 28 '22 04:09

Jens Gustedt