Pointers Flashcards
What do pointers do in C++
They point to a location in memory
What is the ‘&’ character
The reference character; a variable with a leading ‘&’ provides the memory address of that variable
How do you declare a pointer?
With the * character.
ex: int* var or int *var
How do you assign stack memory to a pointer?
int var = 7;
int *point =&var;
How is the * character used to dereference?
When put in front of a pointer, it will provide the value in that memory address.
How do you reassign the value in a pointer?
int *pointer = &var;
*pointer = 13;
Given int pointer ‘pointer’, how do you assign the value in pointer’s memory to variable ‘var’?
int var = *pointer;
How is pointer math performed assuming same pointer type?
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.
Given array y, what does *y return?
The memory address of the first index.
What is created with int *a;?
Movable pointer to writable int
What is created with const int *b;?
Movable pointer to read-only int
What is created with int *const c;?
unmoveable pointer writable int.
What is created with const int *const d;?
unmovable pointer to read-only int.
What is created with int &e = …;?
read/write reference int
What is created with const int &f = …;?
read only reference
How do you use allocated memory with a pointer?
With the ‘new’ keyword.
What are the 2 keyword pairs to remember for heap memory allocation?
new/delete and new[]/delete[]
What type of memory is on the heap?
Dynamic Memory
How do you get access to the typeid function?
#include <typeinfo>
What does typeid return?
A type_info object.
What is useful about type_info objects?
They are comparable.
What is the “this” keyword?
A pointer to an objects self.
How do you interact with data inside a “this” pointer?
With the ‘->’ operator.
Where should dynamic length array int[x] be allocated?
The heap.