Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I delete local pointers in functions? (C++)

Tags:

c++

pointers

A question:

Should I delete pointers which are fetched in functions (not created, just fetched)? Example:

#include <SomeObject>

#define SAFE_DELETE(p) { if (p) { delete (p); (p) = NULL; } }

class DraftObject
{
public:

    DraftObject() : _x(0) {}
    ~DraftObject(){}

    int CalculateSomething()
    {
        AnotherObject* aObj = SomeObject::getInstance()->getAObjPointer();

        /* Do some calculations and etc... */
        _x += aObj->GetSomeIntValue();

        SAFE_DELETE(aObj) // <-- Would you recomend this here?

        return _x;
    }

protected:
    int _x;
};

The aObj would be re-used in other cases as well in the SomeObject instance. I could go on and always call SomeObject::getInstance()->getAObjPointer() for everything I need it for, but SomeObject::getInstance()->getAObjPointer()->GetSomeIntValue() is not as readable as aObj->GetSomeIntValue() in my personal opinion. I know that I would not need to worry if I used something from boost (shared_ptr, weak_ptr or even auto_ptr), but I am more curious about the way this works. Will not deleting the pointer create a memory leak situation or will the deletion of the pointer delete it from the memory so that it will be gone in other scopes (the instance object as well as anywhere else it might be used) ?

Any thoughts?

Cheers.

like image 315
karmalis Avatar asked Jul 16 '12 21:07

karmalis


People also ask

Do you need to delete pointers in C?

No. The only exception to that would be if deltaTime was created with new and it was the responsibility of Update to return the memory (unlikely, and a poor design). like you would with any other pointer? Just because something is a pointer does not mean you should call delete .

Why should we delete pointers?

'Deleting' a pointer will free the memory that it points to regardless if it is a copy or the 'original'. This is because it simply holds a memory address (or a copy of the memory address) and not the actual memory.

What happens when we delete pointer?

The address of the pointer does not change after you perform delete on it. The space allocated to the pointer variable itself remains in place until your program releases it (which it might never do, e.g. when the pointer is in the static storage area).

Do I need to set pointer to null after delete?

Setting pointers to NULL following delete is not universal good practice in C++. There are times when it is a good thing to do, and times when it is pointless and can hide errors. There are plenty of circumstances where it wouldn't help. But in my experience, it can't hurt.


1 Answers

It depends.

If SomeObject::getInstance()->getAObjPointer(); returns a different object each call, probably yes. Otherwise, no. This should be documented.

Also, your "safe delete":

#define SAFE_DELETE(p) { if (p) { delete (p); (p) = NULL; } }

is utterly useless. And ugly. If I saw this in code, I'd go and make fun of the programmer who wrote it. If p is NULL, the delete is safe.

like image 161
Luchian Grigore Avatar answered Oct 14 '22 16:10

Luchian Grigore