1

I'd like to know how C++ is dealing with memory of "objects" created by pointer inside class methods or functions. For example method of class Example

void Example::methodExample()
{

  ExampleObject *pointer = new ExampleObject("image.jpg");

}

Should i somehow delete this or it's automatically removed? Sorry if my question is stupid but i am beginner : P

Mysticial
  • 464,885
  • 45
  • 335
  • 332
fex
  • 3,488
  • 5
  • 30
  • 46

5 Answers5

2

You have two options.

If you use a raw pointer, as you are using in your example, you must manually delete objects that were created with new.

If you don't, you have created a memory leak.

void Example::methodExample()
{
  ExampleObject *pointer = new ExampleObject("image.jpg");

  // Stuff

  delete pointer;
}

Or you may use smart pointers, such as boost::scoped_ptr or C++11's std::unique_ptr.

These objects will automatically delete their pointed-to contents when they are deleted.

Some (like me) will say that this approach is preferred, because your ExampleObject will be correctly deleted even if an exception is thrown and the end of the function isn't reached.

void Example::methodExample()
{
  boost::scoped_ptr<ExampleObject> pointer( new ExampleObject("image.jpg") );

  // Stuff

}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 2
    So can I totally forget about raw pointers and just start using boost smart pointers? – fex Feb 27 '12 at 20:33
  • **My opinion** is that you may opt to use smart pointers whenever you can, and only use raw pointers when you have to. Depending on what you're working on, you may never need to use raw pointers. – Drew Dormann Feb 27 '12 at 21:29
  • @fex: You should forget about raw pointers. You never need them unless you're working on the C++ library. – Neil G Feb 27 '12 at 22:12
  • @NeilG: Not having access/permission to use boost/C++11, using libraries that demand raw pointers in the API, and the **rare** case where profiling shows that raw pointers would provide a significant speed increase would be other situations where raw pointers would make sense. I'm probably overlooking other common examples. – Drew Dormann Feb 27 '12 at 22:52
  • @DrewDormann: It's true that sometimes you don't have permission to use boost/C++11, but then you can always copy the boost/scoped_ptr.h code into your own header file. However, it's wrong to think that raw pointers ever provide a speed increase. – Neil G Feb 27 '12 at 23:03
  • Smart pointers should manage the ownership but then you should pass around references or raw pointers when you are not going to manage their lifetime. Just because shared_ptr has the semantics that you can pass it happily as a parameter doesn't mean you should do so. – CashCow Feb 28 '12 at 00:53
  • 2
    @CashCow: CashCow and DrewDormann are right now that I think about it. Raw pointers are faster than shared_ptr, weak_ptr, etc (though I would still use those until after you've profiled.) But, if all you're doing is dealing with ownership, then there's no penalty for using unique_ptr/scoped_ptr, and these are safer. – Neil G Feb 28 '12 at 01:38
1

You shouldn't be doing your own memory management at all in modern C++. Use unique_ptr or scoped_ptr, which will automatically delete the pointer when they go out of scope.

Neil G
  • 32,138
  • 39
  • 156
  • 257
1

If your object is scoped within the function then your correct construct is not to use a pointer at all but use an automatic object, which should be created like this.

ExampleObject example("image.jpg"); 

You might use a pointer where you are, for example, in an if construct at the time, where the else condition would not construct an object, and then you want to use the object later.

In such a case use an automatic pointer object, preferably unique_ptr if available, boost::scoped_ptr if not, but even the deprecated std::auto_ptr is better than a raw one. For example:

std::unique_ptr<ExampleObject> example;
if( usingAnExample )
{
     example.reset( new ExampleObject("image.jpg") );
}
else
{
   // do stuff
}
// I still need example here if it was created
CashCow
  • 30,981
  • 5
  • 61
  • 92
1

I think the appriate approach for dealing with raw pointers (as you exemplified) is to store the pointer as a member of the class. Then you can allocate memory for this pointer in any method you would like and leave to free the memory on the destructor of the class. Something along these lines:

class Example
{
public:
   Example();
   ~Example();

   void methodExample();

private:
   ExampleObject* pointer;
};

void Example::Example()
: pointer(NULL)
{
}

void Example::~Example()
{
  if (pointer) // release memory only if it was allocated
    delete pointer;
}


void Example::methodExample()
{
  pointer = new ExampleObject("image.jpg");
  // add a safety check to verify if the allocation was successful 
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • If that's really what the OP wants, then (a) initialise `pointer` to null in the constructor; (b) fix `methodExample` to assign to the member; (c) add a non-default copy constructor and copy-assignment operator per the [Rule of Three](http://stackoverflow.com/questions/4172722). But I strongly suspect the OP just wants an automatic object, with no `new`. – Mike Seymour Feb 27 '12 at 17:48
0

You should

delete pointer;

when you no longer need it. The pointer goes out of scope at the end of the function but the memory is still allocated on the heap.

DanS
  • 1,677
  • 20
  • 30