References and Pointers Flashcards
(31 cards)
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.