I have a data member in a class which is a pointer. Also, I have a setter function to dynamically allocate this pointer, let's say, for the purpose of a dynamic array. Before setting new values, I need to delete this pointer to avoid memory leak. I wonder how do I check if this pointer is initialized so that if it is not initialized, I don't need to delete the pointer's memory before setting new values.
Asked
Active
Viewed 115 times
0
-
3set to `nullptr` when it's not in use or do the sane thing and just use a `unique_ptr` to handle the lifetimes for you. Incidentally, calling `delete` on `nullptr` does nothing, anyway. – jaggedSpire Oct 24 '16 at 18:54
-
Do you mean setting default values for the member as `nullptr`? I wonder how this is done in C++?@jaggedSpire – Nicholas Oct 24 '16 at 18:57
-
1yep. just set it to `nullptr` with in class member initialization, in the member initializer list, or in the constructor body – jaggedSpire Oct 24 '16 at 19:00
-
If you use the `unique_ptr` be sure to use the `T[]` variant that's declared like `std::unique_ptr
`, where `T` is the type you're dynamically allocating arrays of. It calls `delete[]` on its internal pointer as you're supposed to with arrays. – jaggedSpire Oct 24 '16 at 19:14 -
@jaggedSpire `std::unique_ptr` sounds great, but just wonder if there's any performance cost (meaning slow down the speed) for using this magic `std::unique_ptr`? – Nicholas Oct 24 '16 at 20:33
-
not really. Unique_ptr is designed for minimal overhead over a raw pointer with manual memory management. – jaggedSpire Oct 24 '16 at 20:36
1 Answers
4
You have two options. First, you can do everything by hand and set it to nullptr
in the constructor. Then before allocating new memory you can check if it's nullptr
and delete it if it's not. What I would recommend instead is using a smart pointer, like std::unique_ptr
. You can call reset
on it every time you need to set it to a new pointer and never worry about memory leaks. It will make sure to delete previously allocated memory, if any.

grigor
- 1,584
- 1
- 13
- 25
-
In your first scenario, you don't need to check for NULL before deleting it. `delete` handles null pointers properly. – Pete Becker Oct 24 '16 at 20:20
-
@PeteBecker 1. You mean I still need to set it to `nullptr` but don't need to check its nullity before `delete`? 2. My pointer might be a dynamic array in the future, then does `delete [] a_pointer;` also work properly on `nullptr`? – Nicholas Oct 24 '16 at 20:30
-
@grigor `std::unique_ptr` sounds great, but just wonder if there's any performance cost (meaning slow down the speed) for using this magic `std::unique_ptr`? – Nicholas Oct 24 '16 at 20:32
-
1Short answer is not really. Longer answer here: http://stackoverflow.com/questions/22295665/how-much-is-the-overhead-of-smart-pointers-compared-to-normal-pointers-in-c – grigor Oct 24 '16 at 20:36
-
@PeteBecker I know that if the pointer in class is not initialized to `nullptr`, it gets a junk value. Then does `delete` even handle this junk value well? Reading your comment, I am thinking now do I really need to initialize the pointer. – Nicholas Oct 24 '16 at 20:38