Questions and CORRECT Answers
Pointers + Memory:
- Pointers
- Void Pointers
- Pointer Casting
- Smart Pointers - CORRECT ANSWER
Pointers - CORRECT ANSWER pointers can point to any sort of memory (basic type ie
int, structure, an array, a class)
pointers hold the address of something in memory and everything in memory has to have an
address.
2 main uses for pointers in C++:
1. as a way of referring to memory allocated dynamically off the heap. malloc() / new operation
2. when passing large chicks of data to function or method, it reduces copying speed. (ex when
passing a large array)
There is no garbage collector so programmer must use free() or delete. If programmer forgets,
program will crash.
Declaring pointers - CORRECT ANSWER declared using *
(check vs code)
ex:
, int *pi; -> pi is pointing to an integer.
double *pd; -> pointer to a double
char *s -> s is pointing to a char
pointers without a specific type:
void *vp;
Reference (Address of) Operator & - CORRECT ANSWER When applied to a variable, &
generates a pointer-to (or address-of) the variable.
ex:
int *p;
int c;
p = &c;
check vs code
Pointers and Assignment - CORRECT ANSWER null pointer: pointer that points at
nothing
- assigning it a value of NULL (c way)
- assign it a value of nullptr (C++ way)
De-reference Operator * - CORRECT ANSWER when we want to access or manipulate
what a pointer is pointing at, we deference pointer first.
refer to pointer.cpp
Bad Uses:
1. Dereferencing a pointer that hasn't been told to point at something yet.