Pointers
Note: I apologize for the lack of visuals, and it may seem lazy, but for me to understand pointers fully, I had to sit and draw out how I visualized what was happening in memory. Looking at visuals did help a little, and visuals can easily be found online, but they also stopped me from drawing them out myself because they were RIGHT there. And because I am trying to get people to understand things the way I understood them, I hope you sit and take the time to draw what’s happening in memory out and ask yourself if it makes sense. And if it doesn’t and you can’t figure out why you can shoot me a message and I’ll even draw you a visual myself. Thanks so much.
Soooooo why do we need pointers? Pointers are used to point to addresses of large structures so we don’t have to make copies of it in our program. This is key to efficient memory management in our code!! They are pretty much useless for ints, chars, and such, but with more efficient data structures that you will hopefully learn in the future (maybe even with help from me), they will be really useful!
-------------------------------------------------------------------------------------------
Memory:
I will use this section to briefly introduce the stack and the heap, but I assume most people coming to look for help with pointers have a decent understanding of stack and heap memory.
Stack:
Memory created locally is called stack memory.
Stack memory does not have deallocated manually. For example, in a function, all memory allocated on the stack is deallocated once the function is completed.
Heap
Memory created dynamically is called heap memory
Heap memory must be deallocated manually. If heap memory is not deallocated manually, it will live on the heap and cause memory leaks and segmentation faults
-------------------------------------------------------------------------------------------
How to use pointers in dynamic memory?
Pointers are initially created in the stack
int *p;
The new operator is used to dynamically allocate memory onto the heap: new + “type”
int *p = new int;
*p = 5;
If memory dynamically allocated is no longer being used, it must be deleted otherwise it is garbage and causes memory leaks. If there is too much garbage on the heap:
space in the heap runs out
and using the new operator to dynamically allocate memory may crash our program
-------------------------------------------------------------------------------------------
What is garbage and how do we prevent garbage on the heap?
Using the delete keyword (Example then explanation in step 2. draw out what it would look like in memory while completing each step)
Dynamically allocate memory on the heap for an integer whose value is 10 and have an integer pointer pointing to it:
int *p = new int(10);
Dynamically allocate memory on the heap for another integer whose value is 5 and have the same integer pointer point to it:
p = new int(5);
Answer these three questions (Don’t worry, you shouldn’t be able to answer all of these yet. Answers in explanation in step 3):
What is the pointer you created pointing to?
What happened to the other integer in the heap?
How do we handle it?
Explanations:
This is how you can dynamically allocate memory in the heap and set its value in one step. Alternatively, you could have done this
int *p = new int();
*p = 10;
Since p has already been declared as an integer pointer, you don’t have to call its type again. All you're doing is reassigning it to point to another place in the heap because you are using the new operator again. Alternatively, you could have done this:
p = new int();
*p = 5;
Answers:
The pointer has been reassigned to point to a new location in memory. It should be pointing at the memory address that is storing the integer value at 5. The pointer cannot point to two memory addresses at once so the other one has been left hanging ¯\_(ツ)_/¯
The other integer in the heap is still there in its address. Nothing is pointing to it so therefore it won’t ever be used unless another pointer is created and points to it or if we reassign the original pointer back to it. (but then it stops pointing to 5).
Ah hah! The magic question. We need to use the delete keyword!!
-------------------------------------------------------------------------------------------
What is the delete keyword and how do we use that?
The delete keyword is used to regain the memory on the heap that we no longer want to use. We call delete when we want to reassign a pointer or to have a pointer no longer point to anything. There should be a 1:1 ratio of new to deletes in your code. In other words: for every new there should be a delete.
Let’s add on to our last example and then I’ll explain as soon as new code is introduced.
Recreate our previous example, but this time remove the garbage off the heap.
int *p = new int(10);
delete p;
*p = new int(5);
Explanation for what happened at 2.:
When we use the delete key we still have our pointer out in the heap, but it no longer points to anything. This is called dangling memory. This becomes a problem when we don’t actually want to reassign our pointer, but to get rid of it completely. WHAT DO WE DO??!?!?!??!?!?!
-------------------------------------------------------------------------------------------
How do we deal with dangling pointers?
OKAY, so I know you’re probably like, “ugh there’s more?” But I promise we’re almost done, and this is a short section so just chill for a bit more, alright? Alright, let’s get it!!
All we have to do take care of a dangling pointer is setting it to NULL or 0. That’s it. Nothing more, nothing less. Ez pz right? Let me explain:
All NULL or 0 does is let the compiler know that our pointer is not referencing to anything. When this occurs, it is no longer pointing to anything in the heap, and there should be no memory leaks! (considering we deallocated the memory).
Let’s remake our example, but this time instead of reassigning the pointer to another address, let’s just have it removed from memory completely!
int *p = new int(10);
delete p;
p = NULL;
-------------------------------------------------------------------------------------------
Summary aka tldr
I hope I was able to point y’all in the right direction! No? Okay, well the lesson is finished anyways so you can deallocate the space in your brain that you made for that pun and replace it with something else :P Let’s just summarize quickly:
Use new to allocate memory on the heap
Deallocate unused memory using delete
Prevent dangling pointers by setting the pointer to NULL or 0 upon deallocation
Thanks, stay tuned for more!
A fun video to review pointers: https://www.youtube.com/watch?v=6pmWojisM_E
-------------------------------------------------------------------------------------------
Corrections
“The new keyword is used dynamically allocated memory on the stack”
Changed stack to heap














