W2 P2: Pointers, References and Arrays Flashcards
Pointer that cannot be dereferenced:
nullptr
Reasons to use synonym pointer types
- does not require a ‘*’ before each identifier
- more readable and less error prone
hexDump() function:
The function hexDump() listed below displays the contents of a region of memory regardless of the type associated with that region. The function receives the region’s address and its size in number of bytes. It casts the generic pointer a to a pointer to an unsigned char and displays the contents of c[i] in hexadecimal notation:
ex.
hexDump(&i, 4);
Outputs:
98 09 00 00
lvalue reference:
an lvalue reference identifies an accessible region of memory.
rvalue reference:
an rvalue reference identifies:
an object near the end of its lifetime
a temporary object or subobject
a value not associated with an object
An lvalue reference requires an initializer unless it:
has external linkage
is a class member within a class definition
is a function parameter or a function return type
std::ref()
returns an lvalue reference to its argument (important of functions in the standard library)
std::move()
returns an rvalue reference to its argument
Range-Based for:
A range-based for is an iteration construct specifically designed for use with collections. The collections can be of any form. This construct steps sequentially through the elements of an array without requiring its size:
int a[]{1, 2, 3, 4, 5, 6}; for (int& e : a) std::cout << e << ' '; std::cout << std::endl; }
A range-based for can infer the type of each element:
for (auto& e : a)