Outline:
- Pointers Explained
- Declaring pointers
- Dereferencing pointers
- Null pointers
- Pointers and Arrays
- General
- Character Pointers
- Incrementing pointers
- Pointer to pointer, and on ...
- Pointers and Functions
- passing by reference
- returning pointers
- pointer to functions
malloc,callocandfree- Memory Leaks
- C++ Pointers
- References
- RAII - resource acquisition is initialization
unique_ptrshared_ptrmake_shared
ℹ Note
All the code sample used below can be found inmain.c.
Pointers are variables that store the address of another variable. - Allow us to indirectly access variables (i.e. we can talk about its address rather than its value)
add diagram here
- More flexible pass-by-reference
- Manipulate complex data structures efficiently, even if their data is scattered in deferent memory locations
- Use polymorphism - calling functions on data without knowing exactly what kind of data it is. (needs example!)
Simply <type> *<var_name>;, e.g.
int *ptr;The pointer can then be initialized to a memory address for a variable, which is found by using &, e.g.
int x = 30;
int *px = &x;ℹ Note
My own preference and the prevalent practice is to put the*just before the name of the variable as opposed to putting it after the type, as some do. The later is also misleading when you have a list of variables in one line, e.g.int *ptr, x, yvs.int* ptr, x, y.
(xandyare just integers but the later may make it look like all are pointers!)
- To dereference a pointer is to get the value of what the pointer is pointing to.
- We use
*<pointer_pointer_var_name>, for example:int x = 30; int *px = &x; *px = 40; // changes x // print memory address of where px is pointing // at (px) and the value in the address (*px) printf("%p -> %d\n", px, *px); // also note that the pointer also is stored // somewhere in memory and we can get its location // by &px, e.g. printf("%p\n", &px); // so printf("%p stores -> %p (px), which stores -> %d (x)\n", &px, px, *px);
-
Any pointer set to
0is called a null pointer, since there's no memory location0, it is an invalid pointer. -
Dereferencing such a pointer leads to a runtime error. One should check whether the pointer is null before dereferencing it.
int *py = 0; // or int *py = NULL; printf("%d\n", *py); // seg-fault!
-
You may ask, what's the point for null pointers? Null pointers are very important for initializing pointers which will point to proper memory addresses later on, but they are not yet determined. If we declared
int *pz;without initializing it, the compiler (GCC in my case), will pointpzto a random memory address ("allocate").int *pz; printf("%d\n", *pz);
-
An array is a list of values arranged sequentially in memory.
-
The variable name of the array is usually a special kind of pointer, it can decay into a pointer; as we will see below:
int arr[] = { 1, 2, 3 }; // `arr` decays into int* (int pointer)
-
Therefore,
arrin the example above is equivalent to anint*.arris a pointer pointing to the beginning of the array. -
To get the first element of the array, we will use
*arr. -
To get the second element of the array, we use
*(arr + 1) -
Therefore to get the nth element of the array we will use
*(arr + n - 1).int arr[] = { 1, 2, 3 }; printf("%d, %d, %d\n", *arr, *(arr + 1), *(arr + 2));
-
Let's look at an example for summing up numbers in an array:
int sumArray(int *arr, int sz) { int sum = 0; for (int i = 0; i < sz; ++i) { sum += *(arr + i); // or sum += arr[i] } return sum; }
sumArraytakes in a pointer to an array and the size of that array.- However, there's not way of telling (AFAIK) that that pointer truly points to an array, it could as well just be an ordinary pointer to an int. For instance of if we gave
x(from our example in the beginning) to this function, it will compile correctly and it may even run without a seg-fault!printf("fake sum -> %d\n", sumArray(px, 3)); // if you thought that's enough, try this! printf("fake sum -> %d\n", sumArray(px, 300)); // 300 contiguous memory addresses // from px are summed up
- This is the same reason why strings (array of chars) have a sentinel value
\0at the end that signifies the end of the array (aka, the null terminator).
ℹ Note
In the next section, we will look at pointer-to-pointer. It is worth noting here that&arrin our example above will be anint**(pointer to pointer, or address of a pointerarr), sincearrisint*.
TBD
TBD
TBD
TBD
- The C library function
void *malloc(size_t size)allocates a block of size bytes of memory, returning a pointer to the beginning of the block. - Defined in
stdlib.h - The function returns a pointer to the allocated memory, or
NULLif the request fails.
TBD
- Leak - failing to release a resource in a timely manner.
- Memory leak occurs when programmers create a memory in heap and forget to delete it.
TBD
Rodney's Challenge:
void allocate(char*** pppArray, int nSize, char* szName, int nLoc) {
// implement
}