Help understanding when to use nullptr

I’m working on a library for the esp32 platform using C++ and PlatformIO , and I’m a bit confused about when to use nullptr within functions. If I take a pointer as a parameter, do I need to set it to nullptr at the end of the function, or is it not instantiated inside the function?

example:

void func(uint32_t* value)
{
    if (*value < 21512)
    {
        // whatever
    }
    value = nullptr;  // do I need this?
}

I understand that I need to call it when removing objects from the heap, but not sure if the function creates a pointer when it is called or not. also not sure if it makes a difference to calling nullptr within the function when calling it like this:

uint32_t* someNum = new uint32_t(25810); // creating a pointer in this scope
func(someNum);  // address passed to func for instantiation of another ptr?

vs calling it like this:

uint32_t someNum = 25810;
func(&someNum); //pointer created inside function?

I can test some of this in actual code but it doesn’t tell me if there’s a memory leak due to not calling nullptr, which is problematic, since it’s an embedded platform.

My guess is that it does need to be set to nullptr before returning, as testing it out seems not to destroy the original pointer passed the first way.

Drink every time you read nullptr.

The problem is when you have a pointer pointing to a value that no longer exists.

Since the pointer is a memory address it is persistent after the referenced object is deleted. It is called a wild pointer I believe, pointint at something it shouldn’t be, in which case you would want to make that pointer null.

I’m still an amateur but I believe this is the explanation you’re after.

Nullptr is just a value like any other. It does not free memory, destroy instances or anything of the sort. Think of it as a zero cast to a pointer. That’s all it is.

((void*)0)

So no, you don’t have to assign null to value. It has no effect and will even be removed by the compiler if you have optimizations enabled.

Same here. It can be useful for debugging to set pointers to nullptr after deleting them, but it doesn’t have any effect.


If you actually want to free memory you have to do so explicitly. Memory allocated with malloc can be deleted using the free function. Memory created with new is freed with delete.

4 Likes

This is not a direct answer to your question, but it is probably relevant to the work you’re doing.

If you are really writing a library in C++, why not use the modern way of using pointers? These days, you should use smart pointers (a.k.a unique and shared pointer) instead. This way you don’t need to think about cleaning up any memory and you can’t access memory you aren’t allowed or has been freed already.

Under the hood, smart pointers are just like normal pointers and you can think of them like a wrapper class. However, they do add many interesting features like counting how many references are pointing to the value and cleaning up the memory once the last reference is gone. And if you really need to, you can always get a raw pointer back, but that should be the exception. :wink:

4 Likes

Ok, think I got it now, much thanks; so is it correct to say that the pointer itself exists in the stack frame of the function, and therefore is gone at the end of that code block anyway?

@comfreak I have looked into smart ptrs a little, but feel I need to understand raw ptrs more before proceeding with those.

As far as canonical C++ style, I have made it a point to use static_cast instead of C-style casts (despite the verbosity), so I’m working my way up.

Yes that’s exactly it. The pointer is on the stack and automatically removed along with the other stack values. The value pointed to is not automatically freed however.

2 Likes

Just to add to this

That’s why smart pointers were invented. They free the memory and let you use the pointer’s value just like a normal stack variable. :slight_smile:

1 Like