Null Pointer in C/C++

In this Lecture, we will discuss about Null Pointer in C++.

You can allocate memory on the heap using the new operator like this.

int * ptr = new int;

It will allocate the memory on the heap and returns a pointer pointing to that memory location. We can initialise our pointer with this value. Now, our pointer ptr points to a memory location on the heap, which contains the integer value. Afterward, we can access that value using the pointer, or we can also change the content of that value using the pointer like this.

*ptr = 22;

std::cout<< *ptr << std::endl;

After usage, we must delete the allocated memory on the heap. For that, we will use the delete operator to delete the memory like this.

delete ptr;

After this line, the memory allocated in the heap will be deleted. However, the pointer ptr will still be pointing to that memory location, which is now an invalid memory location because the memory is already deallocated. So we need to make sure that after the delete operator, the pointer ptr must point to NULL. Like this,

delete ptr;
ptr = NULL;

Now pointer ptr is a NULL Pointer. If we don’t assign NULL t pointer, the pointer will be called a dangling pointer ptr. Now, if we try to access any value using this pointer, it can result in an undefined behavior, because pointer ptr is pointing to a memory location that is already deleted. Therefore, we must initialise the pointer to NULL after deleting the memory. The value of NULL is 0.

Also, while accessing the value using a pointer, we should always first check if the memory is valid or not. For that, we can pass the pointer in an if condition. Like this,



    if (ptr)
    {
        *ptr = 33;
        std::cout<< *ptr << std::endl; 
    }
    

    If the pointer is pointing to a NULL value, it means it’s not a valid value, and we can’t access the value. However, if the pointer contains the NULL, which is also equivalent to zero, this if condition will fail and will not go to this if block.

    When we declare a pointer and don’t initialise it, the default value will be a garbage value. It means the pointer is pointing to some invalid memory location.

    int * ptr2; // Pointer is pointing to invalid memory location
    

    Now, if someone tried to access the value through this pointer, it can result in undefined behavior. Therefore, we should always initialise a pointer while declaring it. Either we need to initialise it with a valid address either on its stack or in heap, or if we plan to assign a valid point at a later stage, then we must initialise the pointer with NULL.

    int * ptr3 = NULL;
    

    While accessing the value, we must check if the given pointer is valid or not like this, and then only should we access the value at that memory location pointed by this pointer.

    If you try to delete a null pointer, then it can also result in undefined behaviour, if data type of Object is a User defained data type. Therefore, before deleting a pointer, we should always check if the pointer is valid or not using the if operator. If the pointer is valid or contains a valid memory location, it will return true, and we can delete the memory. However, if the pointer is a null pointer, then the if statement will return false, and we won’t delete the value.

    The complete example is as follows,

    #include <iostream>
    
    int main()
    {
        int * ptr = new int;
    
        *ptr = 22;
    
        std::cout<< *ptr << std::endl;
    
        delete ptr;
        ptr = NULL;
    
        if (ptr)
        {
            *ptr = 33;
            std::cout<< *ptr << std::endl;
            delete ptr;
        }
    
        return 0;
    }
    

    Output:

    22
    

    Summary

    In this lecture, we learned about Null Pointer in C and C++.

    Scroll to Top