Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register keyword in C++

Tags:

c++

What is difference between

int x=7; 

and

register int x=7; 

?

I am using C++.

like image 655
dato datuashvili Avatar asked Jul 08 '10 19:07

dato datuashvili


People also ask

What is register keyword?

Register keyword tells compiler to store the particular variable in CPU registers so that it could be accessible fast. From a programmer's point of view register keyword is used for the variables which are heavily used in a program, so that compiler can speedup the code.

What is register in C?

Register is a keyword in C which suggests the system to use register as a memory for a variable instead of RAM. This accelerates the reading and writing of memory and enhances the overall performance. Note that using register does not gaurentee the use of system registers.

What is a register variable in C++?

Register variables are similar to automatic variables and exists inside a particular function only. It is supposed to be faster than the local variables. If a program encounters a register variable, it stores the variable in processor's register rather than memory if available.

What is static and register in C?

register is used to store the variable in CPU registers rather memory location for quick access. Static is used for both global and local variables. Each one has its use case within a C program. Extern is used for data sharing between C project files.


2 Answers

register is a hint to the compiler, advising it to store that variable in a processor register instead of memory (for example, instead of the stack).

The compiler may or may not follow that hint.

According to Herb Sutter in "Keywords That Aren't (or, Comments by Another Name)":

A register specifier has the same semantics as an auto specifier...

like image 133
Tom Avatar answered Sep 21 '22 22:09

Tom


According to Herb Sutter, register is "exactly as meaningful as whitespace" and has no effect on the semantics of a C++ program.

like image 23
Fred Larson Avatar answered Sep 17 '22 22:09

Fred Larson