1. Pointer Intro #
Created Saturday 21 December 2019
(Optional - I knew this) Pointers exhaustive from scratch: https://www.youtube.com/watch?v=h-HBipu_1P0&list=PL2_aWCzGMAwLZp6LMUKI3cc7pgGsasm2_
Pointers #
Pointers are variables which hold memory addresses.
- Size of a pointer variable is system(compiler) dependent. A memory address is basically a number. Size of a pointer does not depend on the data type it is pointing to. We can use
sizeof
operator to get the size of a pointer, in bytes. - All variables, we are speaking of, are on RAM. We are not manipulating secondary memory - HDD, SSD, etc.
- Jump(memory blocks to read) is decided by using a symbol table. It has atleast 3 columns. (variable name, type, starting_address). Type decides the jump for reading/writing.
- Symbol table can have two differently named variables for the same address. This is how references are implemented.
- It is wrong to assume that a 32 bit architecture will always use 8 bytes for pointer 4 bytes for an integer. This is mostly true, but not guaranteed, so any code which assumes this is not portable.
Points to - jargon #
- “points to” means that a pointer variable holds the starting_address and jump for the intended memory location.
Declaration: #
- for single pointer variable.
data_type *variable1;
- for multiple pointers on the same line,
data_type *v1, *v2 ...
- This means that we can declare normal and pointer variables on the same line.
NOTE: Always initialize pointer to nullptr
or NULL
or 0
. nullptr
is recommended.
Assignment #
p_variable = &normal_variable
p_variable = pointerVariable
&
is called the “address of” operator, it looks up thestarting_address
of the given variable(from the symbol table).- We can declare a pointer for a pointer using 2 asterisks. Remember that a variable of type “pointer to pointer … to pointer” is the same as a single level pointer variable(if we look at the lookup table). Of course, the content is context specific.
Dereferening(access value) #
- Interacting(i.e reading/writing) value of the variable using its pointer variable is called a dereference. The pointer **needs **to be a valid address and a have a jump(i.e not be a
void
pointer). - void pointers cannot be derefenced(jump size is undefined).
- A pointer with an invalid address(i.e NULL or nullptr or 0) cannot be dereferenced. Hence, NULL is used as a sentinel value.
Jargon for problematic pointers #
Source: https://www.youtube.com/watch?v=uvw5bZZT3pM
- Wild pointer - an uninitialized pointer.
- Dangling pointer - a pointer pointing to a inexistent variable(a variable that has been deallocated aka destroyed aka has gone out of scope). Access can lead to segmentation faults/crashes. Be careful with static pointers - they are initialized only once.
- void pointer - a pointer which is not associated with any data type. The type field(in the symbol table) is undefined. So it cannot be dereferenced(coz we don’t know how much to jump). Use - general purpose pointer, can store address of all types of variables.
- NULL pointer, has an address value of zero. This is not an actual address, so it is used as a sentinel. NULL may be a
char
orint
orlong
depending on the compiler. In modern usage,nullptr
is recommended.