Pointers Flashcards

1
Q

What do pointers do in C++

A

They point to a location in memory

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

What is the ‘&’ character

A

The reference character; a variable with a leading ‘&’ provides the memory address of that variable

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

How do you declare a pointer?

A

With the * character.
ex: int* var or int *var

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

How do you assign stack memory to a pointer?

A

int var = 7;
int *point =&var;

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

How is the * character used to dereference?

A

When put in front of a pointer, it will provide the value in that memory address.

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

How do you reassign the value in a pointer?

A

int *pointer = &var;
*pointer = 13;

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

Given int pointer ‘pointer’, how do you assign the value in pointer’s memory to variable ‘var’?

A

int var = *pointer;

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

How is pointer math performed assuming same pointer type?

A

The pointers will perform arithmatic in the units of the type.
ex: int *one - int *two = 1, assuming they were declared one after the other.

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

Given array y, what does *y return?

A

The memory address of the first index.

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

What is created with int *a;?

A

Movable pointer to writable int

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

What is created with const int *b;?

A

Movable pointer to read-only int

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

What is created with int *const c;?

A

unmoveable pointer writable int.

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

What is created with const int *const d;?

A

unmovable pointer to read-only int.

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

What is created with int &e = …;?

A

read/write reference int

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

What is created with const int &f = …;?

A

read only reference

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

How do you use allocated memory with a pointer?

A

With the ‘new’ keyword.

17
Q

What are the 2 keyword pairs to remember for heap memory allocation?

A

new/delete and new[]/delete[]

18
Q

What type of memory is on the heap?

A

Dynamic Memory

19
Q

How do you get access to the typeid function?

A

#include <typeinfo>

20
Q

What does typeid return?

A

A type_info object.

21
Q

What is useful about type_info objects?

A

They are comparable.

22
Q

What is the “this” keyword?

A

A pointer to an objects self.

23
Q

How do you interact with data inside a “this” pointer?

A

With the ‘->’ operator.

24
Q

Where should dynamic length array int[x] be allocated?

25
Where should fixed length array int[7] be allocated?
The stack.
26
What does the 'sizeof' operator return?
The byte length of a variable / datatype.
27
Given int array 'arr', how do you get the length of the array?
sizeof(arr) / sizeof(arr[0])
28
Given 'int arr[10];', what does 'sizeof(arr);' return?
40
29
What is the difference between new/delete and malloc/free?
new/delete will allocate / deallocate memory as well as the object to put in that memory, malloc/free only deal with the memory and not the object.
30
How is memory cleared on the stack vs the heap?
Memory will only clear up on the stack when it falls out of scope, heap memory can be allocated, deallocated, and re-used at will.