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

You can translate the content of this page by selecting a language in the select box.

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.

Football/Soccer World Cup 2022 Guide and Past World Cups History and Quiz illustrated

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++?
Is it good useful meaningful worthwhile to use register variables in C++?


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.

Invest in your future today by enrolling in this Azure Fundamentals - Microsoft Azure Certification and Training ebook below. This Azure Fundamentals Exam Prep Book will prepare you for the Azure Fundamentals AZ900 Certification Exam.

Microsoft Azure AZ900 Certification and Training

  • 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.

Football/Soccer World Cup 2022 Guide and Past World Cups History and Quiz illustrated

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

With average increases in salary of over 25% for certified individuals, you’re going to be in a much better position to secure your dream job or promotion if you earn your AWS Certified Solutions Architect Associate or AWS Cloud Practitioner certification. Get the books below to for real practice exams:

AWS Certified Solutions Architect Associate SAA-C03 Practice Exams

What are the Greenest or Least Environmentally Friendly Programming Languages?

Machine Learning Engineer Interview Questions and Answers


Football/Soccer World Cup 2022 Guide and Past World Cups History and Quiz illustrated


Read Photos and PDFs Aloud for me iOS
Read Photos and PDFs Aloud for me android
Read Photos and PDFs Aloud For me Windows 10/11
Read Photos and PDFs Aloud For Amazon

My favorite tool for creating blog content about tiny topics is the Jasper AI blog writer.

Get 20% off Google Workspace (Google Meet)  Business Plan (AMERICAS): M9HNXHX3WC9H7YE (Email us for more)

Get 20% off Google Google Workspace (Google Meet) Standard Plan with  the following codes:  96DRHDRA9J7GTN6 (Email us for more))

FREE 10000+ Quiz Trivia and and Brain Teasers for All Topics including Cloud Computing, General Knowledge, History, Television, Music, Art, Science, Movies, Films, US History, Soccer Football, World Cup, Data Science, Machine Learning, Geography, etc....

We know you like your hobbies and especially coding, We do too, but you should find time to build the skills that’ll drive your career into Six Figures. Cloud skills and certifications can be just the thing you need to make the move into cloud or to level up and advance your career. 85% of hiring managers say cloud certifications make a candidate more attractive. Start your cloud journey with these excellent books below:

List of Freely available programming books - What is the single most influential book every Programmers should read

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!