简体   繁体   中英

How do I write a generic function to delete any valid pointer?

Following is my code snippet. I am trying to write generic function to check whether pointer is valid and deleting it.

#include <windows.h>
#include <vector>
#include <map>
#include <string>

using namespace std;

struct testStruct 
{
    int nVal;
    _TCHAR tcVal[256];
    testStruct() 
    {
        wmemset(tcVal, 0, _countof(tcVal));
    }
};

void deletePointer(void *obj)
{
    if (obj)
    {
        delete obj;
        obj = NULL;
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    testStruct *obj = new testStruct;
    wstring *strVal = new wstring();
    vector<wstring> *vecVal = new vector<wstring>;
    map<wstring,wstring> *mapVal = new map<wstring, wstring>;

    //My business logic goes here.
    

    //Finally after all business logic, clearing allocated memory.
    deletePointer(obj);
    deletePointer(strVal);
    deletePointer(vecVal);
    deletePointer(mapVal);

    return 0;
}

While I am not facing any compilation or runtime error, just wanted to confirm, if this is the right way to check and delete multiple points. I don't want to check individual pointer whether it is valid or not before deleting. So calling generic function.

Thanks for your suggestions in advance.

Compilation and runtime errors are not present. Just need confirmation, if this is right way or is there a better way to do this.

No, it's both incorrect and unnecessary

If your compiler doesn't report error on this code, crank up warnings: https://godbolt.org/z/7ranoEnMa . Deleting a void* is Undefined Behaviour, you cannot predict what will be the result. If it's currently not crashing, it will likely crash at some random other use when you will least expect it.

It's unnecessary, because it's perfectly fine to delete nullptr; and your function only checks against that. If you wanted to check if the pointer is actually valid like Nathan Pierson suggests in comment (and you don't assign nullptr to them consistently), that's not possible. You are responsible for your memory management, no if can help if you don't do that correctly throughout the program.

And it's also not necessary, because memory management is already done for you. Containers shouldn't be ever allocated on the heap. Simply do

    wstring strVal;
    vector<wstring> vecVal;
    map<wstring,wstring> mapVal;

And drop the pointers. C++ containers do all the magic by themselves and are generally small by themselves ( sizeof(std::vector) is usually 3*sizeof(void*) ).

Assuming you really need testStruct on the heap rather than in automatic storage, you should use a smart pointer:

std::unique_ptr<testStruct> obj = std::make_unique<testStruct>();

There, it's created, allocated on the heap and will be automatically deleted when obj ends its scope. You don't have to worry about delete ing anything anymore.


If you really want to have a function that delete s objects manually, it should look like this:

template <typename T>
void deletePointer(T*& obj)
{
    delete obj;
    obj = nullptr;
}

It keeps the type of the pointer to be deleted and updates passed pointer with nullptr , so it won't be invalid later on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM