0

I have written following array pointer program in C++. I have deleted the pointer, but i am not able to confirm whether the pointer is deleted or not. Thanks in advance!

#include<iostream>
using namespace std;

int main()
{
    int *p;
    p=new int[10];
    int i=0;

if(!p)
{
    cout<<"\ndynamic memory allocation failed"<<endl;
}

cout<<&p;

for(i=0;i<10;i++)
{
    p[i]=i+1;
}

for(i=0;i<10;i++)
    cout<<"\nvalue of pointer p="<<p[i];


delete[] p;
if(!p)
    cout<<"\n\nmemory cannot be free";
}

2 Answers2

4

How do i check whether the pointer is deleted or not in C++

There is no way to check whether a pointer is deleted or not in C++.

There is also no need to check whether a pointer was deleted. If you get a pointer from a new-expression, and you haven't yet deleted the pointer earlier, then it is safe to assume that delete will release that memory.

Its not about trusting the compiler. I just want to confirm whether it is deleted or not. If its deleted its good, but if it is not deleted then ...

Since it is not possible to test whether a pointer has been deleted or not, the trick is to structure the program such that there is never doubt about the state of the pointer.

The typical solution is to store the pointer as a private member variable of a class described as a "smart pointer", and to never let the post condition of any function of that class to leave the pointer in a deleted state. This establishes a class invariant that guarantees the validity of the pointer throughout the entire lifetime of the object and therefore there is never need to find out when the pointer can be deleted.

The standard library provides smart pointer classes for you, so there's hardly ever need to write delete or delete[] yourself.

In the case of dynamic arrays that you use as an example, you don't need to use any pointers. You can use std::vector instead:

{
    std::vector<int> p(10);
}
// memory was freed; no need to test
eerorika
  • 232,697
  • 12
  • 197
  • 326
1

If you want to be sure that your deleted pointer doesn't contain unsafe values you can make sure that you set it to nullptr after.

delete[] p;
p = nullptr;

If you then want to see if it's deleted, simply check with:

if (p != nullptr)
{
}

Hope this helps!

  • It is giving error: nullptr was not declared in this scope – Anupriya Patil Mar 27 '18 at 09:46
  • @AnupriyaPatil Are you using a C++98 compiler? – Anirban Sarkar Mar 27 '18 at 09:47
  • @AnupriyaPatil that means your compiler is old. You can use 0 instead of nullptr. Consider upgrading however. – eerorika Mar 27 '18 at 09:48
  • @AnirbanSarkar I am using GCC compiler and DEV C++ IDE – Anupriya Patil Mar 27 '18 at 10:02
  • @user2079303 using 0 instead of nullptr works for me! – Anupriya Patil Mar 27 '18 at 10:03
  • @AnupriyaPatil The fact that `0` works either means you have a compiler that only compiles C++ 98/03 or that you may not be using the appropriate flags. Whatever be the case, I would recommend you switch to modern C++. Also please try to use `NULL` instead of the literal `0`, *although arguably they are equally horrible.* – Anirban Sarkar Mar 27 '18 at 10:47
  • 1
    @AnirbanSarkar 0 working does not tell anything about the standard version; It is still a valid null pointer in the latest standard. It's the fact that nullptr doesn't work, that tells that the standard version is less than C++11. – eerorika Mar 27 '18 at 11:47
  • @user2079303 Yes, I meant to say what you stated but thanks for the clarification. – Anirban Sarkar Mar 27 '18 at 12:24