Pointers and Reference Flashcards
What is a pointer used for?
- Inside functions, pointers can be used to access data that are defined outside the function. Those variables may not be in scope so you cant access them by their name.
- Pointers can be used to operate on arrays very efficiently
- We can allocate memory dynamically on the heap or free store
- This memory doesn’t even have a variable name
- The only way to get to it is via a pointer
- With OO. pointers are how polymorphism works
- Can access specific addresses in memory
- Useful in embedded and system applications
What to pay attention to when declaring pointers?
- Always initialize pointers
- Uninitialized pointers contain garbage data and can point anywhere
- Initializing to zero or nullptr(C++ 11) represents address zero
- Implies that the pointer is pointing nowhere
- If you don’t initialize a pointer to point to a variable or function then you should initialize it to nullptr to make it null
- Implies that the pointer is pointing nowhere
What is the sizeof all pointers?
4, the pointer is pointing to an address not the actual object, therefor all pointers have a sizeof 4.
What is the use of untyped VOID* pointers?
The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type:
void* ptr; // ptr is a void pointer
Unlike typed pointers (e.g. int* number) void* pointers can be assigned any type of datatype.
What is dereferencing a pointer?
Dereferencing a pointer means that we are accessing the data the pointer is pointing to.
if score_ptr is a pointer and has a valid address then you can access the data at the address contained in the score_ptr using the dereferencing operator *
example:
int score = 100;
int *scopre_ptr = &score;
cout
How can I convert a void* to a char*?
First I need to cast the void* to a char*
char* ptr = (char*)voidp;
then I can use it just like I would a normal char pointer
How can I allocate dynamic storage (on the heap)
Using the new keyword.
int* int_ptr = nullptr;
int_ptr = new int; // allocated memory to store an integer (32bit or 4 bytes)
Whats the difference between stack and heap?
Stack
A stack is a special area of computer’s memory which stores temporary variables created by a function. In stack, variables are declared, stored and initialized during runtime.
It is a temporary storage memory. When the computing task is complete, the memory of the variable will be automatically erased. The stack section mostly contains methods, local variable, and reference variables.
Heap
The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation.
The heap is not managed automatically for you and is not as tightly managed by the CPU. It is more like a free-floating region of memory.
How can I allocate a array on the heap?
int * array_ptr = new int[size];
How can we free a dynamic storage from the heap?
Using the delete keyword.
What happens if I dereference an array?
int scores[] {100, 95, 95};
cout << *scores << endl;
It’ll point to the first element in the array therefor return 100.
An array is an address to the first element inside the memory.
Given an array
int array[] {1,2,3,4}
how can I access the elements inside the array with subscript notation and Offset notation
subscript:
array[index]
offset:
*(array + index)
this works for pointers pointing to the array the exact same way. An array is just a pointer pointing to the first element
What do the increment and decrement arithmetic operators do with pointers?
They will point the pointer to the next or last object in memory incremented or decremented by sizeof(type). Example:
int_ptr++;
if the type is an int it will either inc or dec by 4 bytes, if its a double it will be 8 bytes etc.
What is the difference between:
const int *score_ptr {&high_score};
and
int *const score_ptr {&high_score};
In the first example when setting the pointer to type const int changing the value the pointer is pointing to won’t be permitted, but changing the address the pointer is pointing to is ok. Example:
\*score\_ptr = 86; //ERROR score\_ptr = &low\_score; //OK
In the second example when setting the pointer itself to a constant, it will be reversed. So setting the value of the reference will be OK but resigning the pointer to a different address won’t.
example:
\*score\_ptr = 86; //OK score\_ptr = &low\_score; //ERROR
When setting both the datatype as well as the pointer to a constant both won’t be able to change after the declaration.
Given the following function:
void double_data(int* int_ptr) {
*int_ptr *= 2;
}
what values can I pass to it?
Either an int pointer pointing to an integer variable:
int* int_ptr = &value;
or a reference to an integer variable:
int value = 10;
double(&data);