Categories: algorithmc++

Is it good useful meaningful worthwhile to use register variables in C++?

Is it good useful meaningful worthwhile to use register variables in C++?

The register keyword has been largely ignored by compilers for decades. In C, it prevents you from taking the address of a variable. But otherwise, most compilers ignore the hint it offers.

STANDARD C++ does not support register variables in 2022. In C register variables serve as a hint to the compiler to use a register rather than a memory address. Prior to C++17, it could also be used as a hint to the compiler in C++ as well, but only some conforming compilers did anything as a result. Starting in C++17, register has become an unused but reserved keyword.

Some compilers still permit it and some may even honor the request. Your programs, however, will not be cross-platform or written in true, standards-compliant C++ if you use the register keyword.

Is it good useful meaningful worthwhile to use register variables in C++?

NOTE:

The situation in C (as opposed to C++) is quite different.

 

“Is it good?”

Usually the answer to that question is that it’s the wrong question, and that you should instead ask “Is it useful/meaningful/worthwhile…?” For example, if you had asked about using the register keyword in C, I would say “There’s nothing wrong about it. It won’t harm your program. It’s just a waste of time.”

It turns out C++ deprecated the register keyword in C++11, and removed it in C++17. As of C++17, the keyword register is now a reserved word with no meaning.

So, the answer to “is it good?” is “No.” Your program will fail to compile as a C++17 program.

By the end of 90s, register was the “R-type” decal of the C programming world. It did about as much for the speed of your program as the decal did for your car. (Possible exception for weak compilers on some embedded platforms.)

In any case, it doesn’t make sense to have a register qualifier on a parameter in a function prototype. It is ignored on any function declaration that is not a definition, similar to top level const.

That is, all four of these are equivalent:

  • void foo(int x);
  • void foo(const int x);
  • void foo(register int x);
  • void foo(register const int x);

Now, both of those are meaningful in a definition. The register keyword stops you from taking an address (in C, but not C++), and const stops you from mutating the variable.

  • void foo(register const int x) {
  • const int *px = &x; // ERROR
  • x = 42; // ERROR
  • }

So, in principle you should be able to delete the unnecessary and useless register keywords from.the prototypes in the header without affecting any aspect of the program’s speed or correctness.

As of C++17 the use of the register qualifier has been removed. It’s now an unused reserved word.

Old? It is outright obsolete and even if you’re coding on an older version you’re advised to not use it for forward compatibility.

It was a hint to the compiler that a variable would be frequently accessed and the the compiler should prefer to use a CPU register to hold it. The downside being you can’t take the address of a register.

In days of yore (pre-millennium) on now arcane and primitive architectures significant improvements could be had from making (typically) the control variable of a for-loop as a register.

I’m sure that’s still true of some embedded platforms. But modern CPUs have multiple tiers of memory cache and the overhead of loading from and storing variables to main memory is less significant.

We no longer really live with the simplistic Von Neumann of CPU and Main Memory with nothing in between.

Also, optimizers have vastly improved and will tend to make good choices for storage on their own.

It was removed from the standard not because its use was doing harm but that it was regarded as having little practical value and the keyword may be useful in the future.

Reference: here

 

Etienne Noumen

Sports Lover, Linux guru, Engineer, Entrepreneur & Family Man.

Recent Posts

A Daily Chronicle of AI Innovations in May 2024

AI Innovations in May 2024

2 days ago

Tips for Ensuring Success Throughout Your Career

For most people, a satisfactory career is essential for leading a happy life. However, ensuring…

5 days ago

Different Career Paths in the Pipeline Industry

The pipeline industry is more than pipework and construction, and we explore those details in…

5 days ago

SQL Interview Questions and Answers

SQL Interview Questions and Answers In the world of data-driven decision-making, SQL (Structured Query Language)…

2 weeks ago

Things To Consider When Switching Internet Providers

Before you make the decision to switch your home’s interest service provider, take the time…

4 weeks ago

A Daily Chronicle of AI Innovations in April 2024

AI Innovations in April 2024. Welcome to the April 2024 edition of the Daily Chronicle,…

1 month ago