0

If I use new operator to create a object like this in a method:

void functionA(){
    ClassA *a = new ClassA();
}

Do I need to use the following code to release it?

   delete a;

Can c++ auto release the ClassA object's memory? when it run out of the functionA scope.

If I write codes like this

void functionA(){
    ClassA a = ClassA();
}

Does a automatically be released?

CrystalJake
  • 897
  • 2
  • 10
  • 18
  • 6
    This is something that is answered in any good [beginner book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – GManNickG Jun 06 '13 at 05:12
  • I think yes, because C++ doesn't have auto garbage collection – pinkpanther Jun 06 '13 at 05:13
  • @pinkpanther, It does, it's called RAII. And C++11 has support for GC. – chris Jun 06 '13 at 05:16
  • @chris but is that default?, is that an external library? I'm talking about with out third party libraries – pinkpanther Jun 06 '13 at 05:18
  • @pinkpanther, The standard library has great RAII options. There's no real *need* for a GC with RAII, but the support is there if you'd rather have that. – chris Jun 06 '13 at 05:28
  • 1
    @pinkpanther: C++11 doesn't *actually* support GC in a generally useful way. It defines parameters for what a GC can do, *if* one exists, and defines some functions for interacting with it. In all common standard library implementations, as far as I know, these functions are no-ops. – Benjamin Lindley Jun 06 '13 at 05:49

5 Answers5

5

For:

void functionA() {
    ClassA *a = new ClassA();
}

You must place delete a; inside the function to clean it:

void functionA() {
    ClassA *a = new ClassA();
    // your other code here
    delete a;
}

Otherwise you will face a memory leak. This is unless you return the a variable somehow or pass it to some other place that frees it.

For:

void functionA(){
    ClassA a = ClassA();
}

a will get 'released' automatically if you define its destructor properly. It is still possible that some field inside a leak if you do not properly clean up in the ClassA destructor.

awesoon
  • 32,469
  • 11
  • 74
  • 99
yanhan
  • 3,507
  • 3
  • 28
  • 38
  • And please note that the semantics of `Class a = ClassA()` are almost certainly different than what the OP expects. – Nik Bougalis Jun 06 '13 at 12:24
3

Do I need to use the following code to release it?

Yes, you have to release every new ed object.

Can c++ auto release the ClassA object's memory?

You could use smart pointers:

#include <memory>

void functionA(){
    std::unique_ptr<ClassA> a_ptr(new ClassA);
}

Memory allocated for a_ptr will be automatically realeased once a_ptr goes out of scope.

If I write codes like this

void functionA(){
    ClassA a = ClassA();
}

Does a automatically be released?

Yes, it does.

awesoon
  • 32,469
  • 11
  • 74
  • 99
3
void functionA(){
    ClassA a = ClassA();
}

Uhm, why write that? It's inefficient, confusing and completely unnecessary. What's wrong with:

void functionA() {
    ClassA a;
}

Generally speaking, if you use new you must use delete when you no longer need the instance that you allocates with new. There are exceptions to that, of course, but you don't need to worry about them now. For now just remember: if you allocated it, with new you own it and must deallocate it with delete.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
1

You can use std::unique_ptr to do this automatically:

void functionA() {
    std::unique_ptr<ClassA> a(new ClassA());
    // don't release
}
awesoon
  • 32,469
  • 11
  • 74
  • 99
perreal
  • 94,503
  • 21
  • 155
  • 181
1

Yes, you need to delete any dynamically allocated raw pointer. If you want to destroy it automatically when going out of scope, you can use some smartpointer included in C++11 standard like [shared_ptr][1] or [unique_ptr][2] or [scoped_ptr][3] in boost library:

 [1]: http://www.cplusplus.com/reference/memory/unique_ptr/
 [2]: http://www.cplusplus.com/reference/memory/shared_ptr/
 [3]: http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/scoped_ptr.htm
fatihk
  • 7,789
  • 1
  • 26
  • 48