cpp_noname
cpp_noname

Reputation: 2071

Pass a register variable by reference

I have seen code where variables with the register keyword are passed by reference into functions.

Version 1:

inline static void swap(register int &a, register int &b) 
{
    register int t = a; 
    a = b; 
    b = t; 
}

Version 2:

inline static void swap(register int a, register int b) 
{
    register int t = a; 
    a = b; 
    b = t; 
}

What are the differences between the two versions?

To my understanding, a and b are kept in registers so the reference operator shouldn't have any effect as the changes made to the values in these registers should persist across the caller-callee boundary, without the use of the reference operator.

Upvotes: 0

Views: 1874

Answers (2)

INIM
INIM

Reputation: 55

For a C++ program, the memory of a computer is like a succession of memory cells, each one byte in size, and each with a unique address. These single-byte memory cells are ordered in a way that allows data representations larger than one byte to occupy memory cells that have consecutive addresses.

This way, each cell can be easily located in the memory by means of its unique address. For example, the memory cell with the address 1776 always follows immediately after the cell with address 1775 and precedes the one with 1777, and is exactly one thousand cells after 776 and exactly one thousand cells before 2776.

When a variable is declared, the memory needed to store its value is assigned a specific location in memory (its memory address). Generally, C++ programs do not actively decide the exact memory addresses where its variables are stored. Fortunately, that task is left to the environment where the program is run - generally, an operating system that decides the particular memory locations on runtime. However, it may be useful for a program to be able to obtain the address of a variable during runtime in order to access data cells that are at a certain position relative to it.

Upvotes: -1

Michael Graczyk
Michael Graczyk

Reputation: 4965

In C programs, you cannot take the address of a register variable.

register int x;
int * p = &x; // Compiler error

This is sometimes useful in macros to prevent clients from taking the address of something that should only be used as a value.

The use of register is deprecated in the C++11 standard (see [depr.register]). In C++ it is legal to take the address of a register variable, but it not legal in the latest revision of the C++11 standard to declare an alignment for a register variable with alignas. See 7.6.2 Alignment specifier

Other than preventing the use of alignas() and causing a syntax error when used outside local, register does nothing in C++. Since it's deprecated and because I can't imagine any reason you would want to prevent the alignment of variable used inside a macro, you should avoid using register in C++ code.

To answer the question: In C++ there is no difference between your code and the equivalent code with register removed, so your "two versions" are different in the obvious way.

Upvotes: 4

Related Questions