Dangling Pointer and Memory Leak in C++ When Using Pointer (Random Programming Problems Part 2)

Pointer is a very powerful feature of C++. It’s like having an instrument of god to perform divine tasks (Ok, I might be exaggerating it a little bit here but you get the point). However, with great power comes great responsibility. This article will discuss about common memory safety issues when using pointer in your project.

Tam Nguyen
5 min readSep 7, 2021
Take care of you memory.

What is pointer?

Let’s just recap a little bit about pointer. As you know every variable is a memory location. This memory location has an address which can be accessed using ampersand (&) operator.

This will results in:

Address of myvar: 0x7ffc576b3bbc
Value of myvar: 10

A pointer is a variable which stores the address of another variable. It means that you can store the address of the example above using a pointer like this.

This one will output the following:

Address of myvar: 0x7ffc669e6704
Value of myvar: 10
Value of mypointer: 0x7ffc669e6704
Value pointed to by mypointer: 10

At this point, you probably think: “Man, this guy talk so much about basic academic stuffs, I didn’t come here for a lecture in how to use pointer. Maybe next time I’ll find another programming series.”

Blah… blah… blah…

Ok… ok chill… You know you are not the only person who read this blog right? (well, I hope so…). There are people that doesn’t always use pointer in C++ and they are learning all the new things here. So a recap, in my point of view, is necessary. But yes I also agree that we have enough of that, let’s move on to the next part.

What is memory safety?

When we talk about building secure application with C++, we often focus on memory safety. What this mean is , in all possible executions of a program, there is no access to invalid memory. In this article we will only focus on dangling pointer and memory leak.

Usually, in some other languages, for example Java. There is a garbage collector that helps you handling memory issues. However, in C++ you have to do it on your own. There is costs and benefits to both of the designs, but we will not discussing that aspect here because it is a long and details subject for another day. But cheer up, we can still have a meme about it.

Java vs. C++

What is memory leaks?

Memory leaks occur when new memory is allocated dynamically and never deallocated. In C++, new memory is usually allocated by the new operator and deallocated by the delete or the delete [] operator. The problem with memory leaks is that they accumulate over time and, if left unchecked, may cripple or even crash a program.

It is not always that simple to detect memory leak. When you are coding with pointer, things can get a bit tricky.

A pointer you declare in a function is allocated on the stack, but the dynamic variable it points to is allocated on the heap. If you forget to delete it, it will remain after the program exits from the function.

In this example below, the memory is reallocated using the pointer ptr . What happens is that the first allocation is lost irretrievably, and so are the 30 bytes that it pointed to. Now they’re impossible to free, and you have a memory leak.

What is a dangling pointer?

A dangling pointer points to memory that has already been freed. The memory address storing the data is no longer valid. Any attempt to modify or access this will cause a Segmentation fault. In this invalid_pointer() example, the ptr2 is called although it has been deleted.

Local function can also create dangling pointer if you are returning an address which was a local variable that has gone out of scope by the time it is returned to the calling function.

In this example above, ptr pointing to my_array[10] , which is no longer exist after exiting out_of_scope_pointer() . This might result in an Undefined behaviour.

Take care of your memory safety

To put it simple. For every new you should use a delete so that you free the same memory you allocated. Following this instruction will help stay away from some common memory issues.

However, as we discuss above in this article, things can get complicated when you write 10000+ lines of code. This can make those troubles very difficult to find.

But hey, it’s not the end of the world. As you grow in your skills, you’ll find that you naturally write code which protects your applications from memory leaks. Just keep in mind the best practices when programming. Even Jerry knows how to get gudddd!

A bit of small talk

Oh hello there, random stranger. You know I was writing a different article and was planning to make it the part 2 of this series. That’s why it takes such a long time for me to come back. However, at some point I believe that article should be on another series itself. Well, maybe some of these days you’ll find me publishing a series about hosting and cloud system, bla… bla… bla… you know? The boring stuffs.

Recently I got to a point in my job where I am a bit burnt out. I think I might have to do something about it soon. These days I entertain myself by doing some Leetcode, learning some C++ and algorithm. You should try it too. I read somewhere that learning new things make you happy. That’s why properly this series will dive deep into some more C++ related problems.

Anyway I think it’s good to know that there is still something out there for us to reach out. So once in a while we learn that we don’t have to be depressed about what we cannot control in our life. We can move on and do something else. You don’t have to do big things to create meaning in your life. Just enjoy the little things.

Thank you, random stranger.

--

--

Tam Nguyen

“Remember Red, hope is a good thing, maybe the best of things, and no good thing ever dies.”