References and Pointers Flashcards
Compound / Composite Data Types
data types that can be constructed from fundamental data types or other compound data types
Value category of an expression
this indicates whether the expression resolves to a value, a function, or an object of some kind
lvalue
an expression that evaluates to a function or an object that has an identity (e.g. an identifier or an identifiable memory address)
two subtypes of lvalues
modifiable and non-modifiable
rvalue
an expression which is not an lvalue. Includes literals (except string literals) and return values of functions or operators when returned by value
reference
an alias for an existing object
once the reference has been defined, any operation on the reference is applied to the object being referenced
Two types of references in C++
lvalue references and rvalue references
lvalue reference
commonly called a reference
acts as an alias for an existing lvalue (such as a variable)
term for when a reference is initialized with an object or function
bound to it
referent
object or function being referenced by a reference
why are lvalue references occasionally called lvalue references to non-const
Lvalue references can’t be bound to non-modifiable lvalues or rvalues (otherwise you’d be able to change those values through the reference, which would be a violation of their const-ness)
references: reseated
Once initialized, a reference in C++ cannot be reseated, meaning it can not be changed to reference another object.
dangling reference
When an object being referenced is destroyed before a reference to it, the reference is left referencing an object that no longer exists.
Accessing a dangling reference leads to undefined behavior.
lvalue reference to a const value aka reference to const aka const reference
By using the const keyword when declaring an lvalue reference, we tell an lvalue reference to treat the object it is referencing as const.
Const references can bind to modifiable lvalues, non-modifiable lvalues, and rvalues.
temporary object / unnamed object / anonymous object
an object that is created for temporary use (and then destroyed) within a single expression.
pass by reference
declare a function parameter as a reference (or const reference) rather than as a normal variable. When the function is called, each reference parameter is bound to the appropriate argument.
Because the reference acts as an alias for the argument, no copy of the argument is made.
address-of operator
&
returns the memory address of its operand
dereference operator
*
Dereferences a pointer by returning the value at a given memory address as an lvalue
int value {10};
int *ptr { &value }; // ptr hold address of value
int dereferencedValue { *ptr };
pointer
is an object that holds a memory address (typically of another variable) as its value. This allows us to store the address of some other object to use later. Like normal variables, pointers are not initialized by default.
wild pointer
A pointer that has not been initialized
dangling pointer
is a pointer that is holding the address of an object that is no longer valid (e.g. because it has been destroyed).
nullptr
Besides a memory address, there is one additional value that a pointer can hold: a null value. When a pointer is holding a null value, it means the pointer is not pointing at anything.
represents a null pointer literal
Rules for working with pointers
A pointer should either hold the address of a valid object, or be set to nullptr. That way we only need to test pointers for null, and can assume any non-null pointer is valid.
pointer to a const value
(sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value.
const pointer
is a pointer whose address can not be changed after initialization.
const pointer to a const value
can not have its address changed, nor can the value it is pointing to be changed through the pointer
pass by address
instead of providing an object as an argument, the caller provides an object’s address (via a pointer). This pointer (holding the address of the object) is copied into a pointer parameter of the called function (which now also holds the address of the object). The function can then dereference that pointer to access the object whose address was passed.
Return by reference
returns a reference that is bound to the object being returned, which avoids making a copy of the return value.
Using return by reference has one major caveat: the programmer must be sure that the object being referenced outlives the function returning the reference. Otherwise, the reference being returned will be left dangling (referencing an object that has been destroyed), and use of that reference will result in undefined behavior.
If a parameter is passed into a function by reference, it’s safe to return that parameter by reference.
If a function returns a reference, and that reference is used to initialize or assign to a non-reference variable…
…the return value will be copied (as if it had been returned by value).
auto keyword for variables
Type deduction for variables (via the auto keyword) will drop any reference or top-level const qualifiers from the deduced type. These can be reapplied as part of the variable declaration if desired.
Return by address
works almost identically to return by reference, except a pointer to an object is returned instead of a reference to an object