C++ Double Address Operator? (&&)
This is C++11 code. In C++11, the &&
token can be used to mean an "rvalue reference".
Why address-of operator ('&') can be used with objects that are declared with the register storage class specifier in C++?
The restriction on taking the address was deliberately removed in C++ - there was no benefit to it, and it made the language more complicated. (E.g. what would happen if you bound a reference to a register
variable?)
The register
keyword hasn't been much use for many years - compilers are very good at figuring out what to put in registers by themselves. Indeed in C++ the keyword is currently deprecated and will eventually be removed.
What is the meaning of '*' and '&'?
This is possibly one of the most confusing things in Go. There are basically 3 cases you need to understand:
The &
Operator
&
goes in front of a variable when you want to get that variable's memory address.
The *
Operator
*
goes in front of a variable that holds a memory address and resolves it (it is therefore the counterpart to the &
operator). It goes and gets the thing that the pointer was pointing at, e.g. *myString
.
myString := "Hi"
fmt.Println(*&myString) // prints "Hi"
or more usefully, something like
myStructPointer = &myStruct
// ...
(*myStructPointer).someAttribute = "New Value"
*
in front of a Type
When *
is put in front of a type, e.g. *string
, it becomes part of the type declaration, so you can say "this variable holds a pointer to a string". For example:
var str_pointer *string
So the confusing thing is that the *
really gets used for 2 separate (albeit related) things. The star can be an operator or part of a type.
Use of the & operator in C++ function signatures
A reference is not a pointer, they're different although they serve similar purpose.
You can think of a reference as an alias to another variable, i.e. the second variable having the same address. It doesn't contain address itself, it just references the same portion of memory as the variable it's initialized from.
So
string s = "Hello, wordl";
string* p = &s; // Here you get an address of s
string& r = s; // Here, r is a reference to s
s = "Hello, world"; // corrected
assert( s == *p ); // this should be familiar to you, dereferencing a pointer
assert( s == r ); // this will always be true, they are twins, or the same thing rather
string copy1 = *p; // this is to make a copy using a pointer
string copy = r; // this is what you saw, hope now you understand it better.
Meaning of referencing and dereferencing in C
Referencing means taking the address of an existing variable (using &) to set a pointer variable.
In order to be valid, a pointer has to be set to the address of a variable of the same type as the pointer, without the asterisk:
int c1;
int* p1;
c1 = 5;
p1 = &c1;
//p1 references c1
Dereferencing a pointer means using the * operator (asterisk character) to retrieve the value from the memory address that is pointed by the pointer:
NOTE: The value stored at the address of the pointer must be a value OF THE SAME TYPE as the type of variable the pointer "points" to, but there is no guarantee this is the case unless the pointer was set correctly. The type of variable the pointer points to is the type less the outermost asterisk.
int n1;
n1 = *p1;
Invalid dereferencing may or may not cause crashes:
- Dereferencing an uninitialized pointer can cause a crash
- Dereferencing with an invalid type cast will have the potential to cause a crash.
- Dereferencing a pointer to a variable that was dynamically allocated and was subsequently de-allocated can cause a crash
- Dereferencing a pointer to a variable that has since gone out of scope can also cause a crash.
Invalid referencing is more likely to cause compiler errors than crashes, but it's not a good idea to rely on the compiler for this.
References:
http://www.codingunit.com/cplusplus-tutorial-pointers-reference-and-dereference-operators
& is the reference operator and can be read as “address of”.
* is the dereference operator and can be read as “value pointed by”.
http://www.cplusplus.com/doc/tutorial/pointers/
& is the reference operator
* is the dereference operator
http://en.wikipedia.org/wiki/Dereference_operator
The dereference operator * is also called the indirection operator.
Related Topics
What Does "Void *(*)(Void *)" Mean in C++
Cannot Convert 'Const Char*' to 'Lpcwstr {Aka Const Wchar_T*}'
Windows Unicode C++ Stream Output Failure
How to Download a File with Winhttp in C/C++
How to Write to Middle of a File in C++
Diamond Inheritance Lowest Base Class Constructor
How to Optimize Matrix Multiplication (Matmul) Code to Run Fast on a Single Processor Core
How to Use Cin.Fail() in C++ Properly
Array Decay to Pointers in Templates
C Function Pointers with C++11 Lambdas
Can't Modify Char* - Memory Access Violation
C++ Delete Pointer Issue, Can Still Access Data
Why Are Arguments Which Do Not Match the Conversion Specifier in Printf Undefined Behavior
Why the Sizeof(Bool) Is Not Defined to Be One, by the Standard Itself