开发者

can a pointer be deleted several times c++?

If I have the following example:

test.h

class MyClass
{
public:
MyClass();
std::string name1;
std::string name2;
std::string type1;
std::s开发者_开发问答tring type2;

void method1(MyClass &obj1);
void method2(MyClass &obj2);
}

test.cpp

MyClass *mainObject = new MyClass();

MyClass::MyClass()
{
}
void MyClass::method1((MyClass &obj1)
{
//do stuff
mainObject=&obj1; //we populate some of the MyClass variables


}
void MyClass::method2((MyClass &obj2)
{
//do stuff
mainObject=&obj2; //we populate the rest of MyClass variables
}

When should I delete mainObject inside test.cpp? Should I create a destructor in order for the client to delete it?


This is a good example that's best solved by not thinking about it yourself.

Use a shared_ptr<MyClass> mainObject; (either the new C++11 or the Boost version). It will do the delete for you.

Mind you, method1() and method2() should take their argument by shared_ptr too. Currently, they're doing a very bad thing: deleting an object that's passed by reference.


Deleting a pointer variable (pointing to non-0) several times is worse than not deleting it. Because the former can cause hard to find bugs and undefined behavior.

Your code is not correctly written. You should delete mainObject; as soon as you try to assign it with &obj1 or &obj2. But make sure that you do it only first time. Don't delete the pointer if it's pointing to obj1 or obj2.

I feel from this question and previous question of yours, that you are coming from Java/C# background. Better to read a good book on C++ first, you will learn that most of the time you don't need new/delete.


You should delete the pointer when you are done using the object it points to. You should not delete a pointer twice while it is pointing to a single object. You should not delete a pointer if it is pointing to an object that you didn't dynamically allocate with new.


I think that I'd go a slightly different way.

Like this:

test.h

class MyClass
{
public:
  MyClass();
  std::string name1;
  std::string name2;
  std::string type1;
  std::string type2;

  void method1(MyClass &obj1);
  void method2(MyClass &obj2);
}

test.cpp

MyClass mainObject; // default c-tor called automatically.

MyClass::MyClass()
{
}
void MyClass::method1(MyClass & obj1)
{
  //do stuff

  //we populate some of the MyClass variables
  mainObject.name1=obj1.name1;
  mainObject.type1=obj2.type1;
}
void MyClass::method2(MyClass & obj2)
{
  //do stuff

  //we populate more of the MyClass variables
  mainObject.name2=obj1.name2;
  mainObject.type2=obj2.type2;
}

There is no simple way to only populate part of your object without specifying which parts.

But, otherwise, if you don't make mainObject a pointer then you don't need to allocate space for it, that's done automatically. (But, I should object to use of globals unless they are REALLY needed!)

This implementation of what I THINK you're trying to do will completely avoid the need for use of the heap, no need for new/delete.


There should always be a logical owner of any resource, and that owner should delete the resource.

There are cases where it makes sense to have shared ownership, and that is what boost::shared_ptr and similar solutions are for. The last one to give up ownership is then the one to delete the resource.


From all comments it looks like you might actually want the following:

static MyClass mainObject; // Not a pointer. Local to test.cpp

void MyClass::method1()
{
  //do stuff
  mainObject=*this; // Make a copy of the last object modified.
}
void MyClass::method2()
{
  //do stuff
  mainObject=*this; // Make a copy of the last object modified.
}

In this way, whether you call foo.method1() or bar.method2, the object on the left side of the . is copied to mainObject. No pointer funkyness needed at all, no new and no delete.


When should I delete mainObject inside test.cpp?

When it is no longer used.

Should I create a destructor in order for the client to delete it?

You only have to create a destructor if some resources of class MyClass have to be released - this is not the case with the shown code. The one you should release (=delete) is mainObject. But anyway, method1(..) and method2(..) are overwriting the mainObject pointer which leads to a dangling pointer (you can't reach the object anymore).

[EDIT]
To answer your question:

can a pointer be deleted several times c++?

  1. Pointers are typically not allocated with new - only the objects they pointing to.
  2. If you mean "can delete be called several times on the same pointer?" the answer is no and would lead to UB. delete on a pointer which is zero is defined and legal.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜