Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Which Situations To Delete A Pointer

Tags:

c++

My following question is on memory management. I have for example an int variable not allocated dynamically in a class, let's say invar1. And I'm passing the memory address of this int to another classes constructor. That class does this:

class ex1{
    ex1(int* p_intvar1)
    {
       ptoint = p_intvar1;
    }

    int* ptoint;
};

Should I delete ptoint? Because it has the address of an undynamically allocated int, I thought I don't need to delete it.

And again I declare an object to a class with new operator:

objtoclass = new ex1();

And I pass this to another class:

class ex2{
    ex2(ex1* p_obj)
    {
       obj = p_obj;
    }

    ex1* obj;
};

Should I delete obj when I'm already deleting objtoclass?

Thanks!

like image 531
tam bakaka Avatar asked Sep 25 '15 10:09

tam bakaka


People also ask

What happens if you access a pointer after deleting it?

Important: you access a pointer after deleting it. DO NOT DO THIS. This is a common memory management mistake. Once you call delete with a pointer, your program no longer has the right to use the memory the pointer points to. Thanks, Albatross! That was very clear.

How to call delete on a pointer that is allocated dynamically?

You can call delete only on memory you allocated dynamically (on the heap) using the new operator. 3. The above did nothing at all. You didn't free anything, as the pointer pointed at NULL. The following shouldn't be done: You pointed it at NULL, leaving behind leaked memory (the new int you allocated).

How to delete a pointer that points to null in C++?

C++ allows that you try to delete a pointer that points to null but it doesn't actually do anything, just doesn't give any error. Pointers are similar to normal variables in that you don't need to delete them. They are removed from memory at the end of a functions execution and/or the end of the program.

What is a pointer in C++?

Pointers are similar to normal variables in that you don't need to delete them. They are removed from memory at the end of a functions execution and/or the end of the program. You can however use pointers to allocate a 'block' of memory, for example like this: This will allocate memory space for 20000 integers.


2 Answers

Because it has the address of an undynamically allocated int I thought I don't need to delete it.

Correct.

Should I delete obj when I'm already deleting objtoclass?

No.

Recall that you're not actually deleting pointers; you're using pointers to delete the thing they point to. As such, if you wrote both delete obj and delete objtoclass, because both pointers point to the same object, you'd be deleting that object twice.

I would caution you that this is a very easy mistake to make with your ex2 class, in which the ownership semantics of that pointed-to object are not entirely clear. You might consider using a smart pointer implementation to remove risk.

like image 188
Lightness Races in Orbit Avatar answered Sep 18 '22 16:09

Lightness Races in Orbit


just an appendix to the other answers

You can get rid of raw pointers and forget about memory management with the help of smart pointers (shared_ptr, unique_ptr).

The smart pointer is responsible for releasing the memory when it goes out of scope.

Here is an example:

#include <iostream>
#include <memory>

class ex1{
public:
    ex1(std::shared_ptr<int> p_intvar1)
    {
        ptoint = p_intvar1;
        std::cout << __func__ << std::endl;
    }
    
    ~ex1()
    {
        std::cout << __func__ << std::endl;
    }
private:
    std::shared_ptr<int> ptoint;
};

int main()
{
    std::shared_ptr<int> pi(new int(42));
    std::shared_ptr<ex1> objtoclass(new ex1(pi));

    /* 
     * when the main function returns, these smart pointers will go
     * go out of scope and delete the dynamically allocated memory
     */ 

    return 0;
}

Output:

ex1
~ex1
like image 30
sergej Avatar answered Sep 18 '22 16:09

sergej