Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check invalid address/deleted pointer?

I’m pretty new to c++ and I’m stuck at this problem.
I append a struct pointer(Bar*) to a vector and that struct have a pointer class member(Foo*).

struct Bar
{
    const int  var{ 0 };

    Foo* m_foo{ nullptr };
};

std::vector<Bar*> list;

int main()
{
  Bar* p_Bar = new Bar;
  p_Bar->m_foo = new Foo;
  list.emplace_back(p_Bar);
}

I have a thread that checks validity of these pointers. Once I delete those pointers, vector element should exist and I should check whether both pointers are valid or not.
When any of them is invalid, I should erase that vector element(after check I mean).
Here’s my try:

#include <iostream>
#include <vector>
#include <thread>

class Foo
{
public:
    Foo() {};

    const int var{ 5 };
};

struct Bar
{
    const int  var{ 0 };

    Foo* m_foo{ nullptr };
};

std::vector<Bar*> list;

bool _check = true;
void Check()
{
    while (_check)
    {
        for (int c = 0; c < (int)list.size(); c++)
        {
            Bar* p = list[c];

            if (p)
            {
                if (p->m_foo)
                {
                    std::cout << "m_foo->var:" << p->m_foo->var << "\nEnter anything to delete the element: ";
                }
                else
                {
                    std::cout << "m_foo was nullptr";
                }
            }
            else
            {
                std::cout << "Element was invalid";
            }
        }
        std::this_thread::sleep_for(std::chrono::duration(std::chrono::seconds(2)));
    }
}

int main()
{
    Bar* p_Bar = new Bar;
    p_Bar->m_foo = new Foo;

    list.emplace_back(p_Bar);

    std::thread thread1(Check);
    thread1.detach();

    std::string t;
    std::cin >> t;

    if (list[0]->m_foo)
        delete list[0]->m_foo;
    if (list[0])
        delete list[0];
    list.clear();

    std::cin >> t;

    _check = false;

    return 0;
}

To check whether the pointer was deleted or not, I should use NULL or nullptr which actually means 0.
But once the pointer is deleted, the address will be something like this 0xFFFFFFFFFFFFFFFF and IDE will throw this kind of exception:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Exception thrown: read access violation.
p->m_foo was 0xFFFFFFFFFFFFFFFF.

How to check whether the pointer’s deleted/pointer’s address is valid or not?

>Solution :

How to check whether the pointer’s deleted/pointer’s address is valid or not?

It isn’t possible to check whether a pointer is valid or invalid. If a pointer is valid or null, then you can check which one it is. If a pointer is invalid, then the result of the comparison will be unspecified.

Besides comparing an invalid pointer, your program has another bug: You’re deleting in one thread, and accessing the pointer in another without synchronising the operations. Similarly, you’re accessing the elements of the vector while its elements are removed in another thread. The behaviour of the program is undefined.


P.S. Avoid owning bare pointers.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading