Home > OS >  How to check invalid address/deleted pointer?
How to check invalid address/deleted pointer?

Time:03-14

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:

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?

CodePudding user response:

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.

CodePudding user response:

You could use automatic_ptr class instead classic in-build pointer. You can develope yourself, or use eg. std::unique_ptr and std::shared_ptr. Principe of this classes is, that pointer is encalupsed in thos class and class count external links (pointers...) and store it to internal attribute. In each book of C is example for it.

  • Related