References and Pointers Flashcards

1
Q

Compound / Composite Data Types

A

data types that can be constructed from fundamental data types or other compound data types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Value category of an expression

A

this indicates whether the expression resolves to a value, a function, or an object of some kind

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

lvalue

A

an expression that evaluates to a function or an object that has an identity (e.g. an identifier or an identifiable memory address)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

two subtypes of lvalues

A

modifiable and non-modifiable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

rvalue

A

an expression which is not an lvalue. Includes literals (except string literals) and return values of functions or operators when returned by value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

reference

A

an alias for an existing object

once the reference has been defined, any operation on the reference is applied to the object being referenced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Two types of references in C++

A

lvalue references and rvalue references

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

lvalue reference

A

commonly called a reference

acts as an alias for an existing lvalue (such as a variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

term for when a reference is initialized with an object or function

A

bound to it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

referent

A

object or function being referenced by a reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

why are lvalue references occasionally called lvalue references to non-const

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

references: reseated

A

Once initialized, a reference in C++ cannot be reseated, meaning it can not be changed to reference another object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

dangling reference

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

lvalue reference to a const value aka reference to const aka const reference

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

temporary object / unnamed object / anonymous object

A

an object that is created for temporary use (and then destroyed) within a single expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

pass by reference

A

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.

17
Q

address-of operator

A

&

returns the memory address of its operand

18
Q

dereference operator

A

*

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 };

19
Q

pointer

A

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.

20
Q

wild pointer

A

A pointer that has not been initialized

21
Q

dangling pointer

A

is a pointer that is holding the address of an object that is no longer valid (e.g. because it has been destroyed).

22
Q

nullptr

A

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

23
Q

Rules for working with pointers

A

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.

24
Q

pointer to a const value

A

(sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value.

25
Q

const pointer

A

is a pointer whose address can not be changed after initialization.

26
Q

const pointer to a const value

A

can not have its address changed, nor can the value it is pointing to be changed through the pointer

27
Q

pass by address

A

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.

28
Q

Return by reference

A

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.

29
Q

If a function returns a reference, and that reference is used to initialize or assign to a non-reference variable…

A

…the return value will be copied (as if it had been returned by value).

30
Q

auto keyword for variables

A

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.

31
Q

Return by address

A

works almost identically to return by reference, except a pointer to an object is returned instead of a reference to an object