0

I have a code like:

class A{ public: void method1 (){ // do something } }; class B{ public: void method2 (){ // do something } }; main(int argc, char* argv[]) { A a ; a.method1(); // free object a B b ; b.method2(); } 

Now, before creating b object, I want to free memory alocated by a. Can anybody help me how to do that ?

4
  • 2
    Put it in its own scope. By the way, you should stop using GC terminology with C++. It doesn't have one. Commented Dec 10, 2012 at 12:33
  • C++ is supposed to produce no garbage to start with! (As per creator of C++) EDIT: The responsibility is fully yours, though. If you can't handle it, try another language. Commented Dec 10, 2012 at 12:37
  • 1
    @ActiveTrayPrntrTagDataStrDrvr Languages don't create garbage, Programmers do! Commented Dec 10, 2012 at 12:38
  • Then you'll be shooting a foot and blowing the whole leg. Another feature of the language. Commented Dec 10, 2012 at 12:47

4 Answers 4

6

You just need to add additional scope {,} , Since a is a automatic/local object, it will be destroyed automagically once the scope in which it is declared ends.

main(int argc, char* argv[]) { { //<-------------- A a ; a.method1(); } //<------------- B b ; b.method2(); } 

This is also popularly known as RAII in C++.

Sign up to request clarification or add additional context in comments.

Comments

4

If you need to do this, I think I should first point out this is a code smell. Destruction of an object shouldn't be a prerequisite for a new one being created. If it is, you're better off abstracting that away.

Here goes though:

main(int argc, char* argv[]) { { A a ; a.method1(); }// free object a B b ; b.method2(); } 

Comments

0

You should use a new scope and automatic objects, but here is also an example of how to do it using dynamic allocation.

main(int argc, char* argv[]) { A* a = new A(); a.method1(); delete a; B* b = new B(); b.method2(); delete b; } 

1 Comment

You might have misunderstood what was being asked. Also: RAII.
0

That depends on how you memory is allocated by a.

for the code you pasted:

g++ -gstabs -o a cpptest.cpp objdump -d ./a 0000000000400554 <main>: 400554: 55 push %rbp 400555: 48 89 e5 mov %rsp,%rbp 400558: 48 83 ec 20 sub $0x20,%rsp 40055c: 89 7d ec mov %edi,-0x14(%rbp) 40055f: 48 89 75 e0 mov %rsi,-0x20(%rbp) 400563: 48 8d 45 ff lea -0x1(%rbp),%rax 400567: 48 89 c7 mov %rax,%rdi 40056a: e8 13 00 00 00 callq 400582 <_ZN1A7method1Ev> 40056f: 48 8d 45 fe lea -0x2(%rbp),%rax 400573: 48 89 c7 mov %rax,%rdi 400576: e8 11 00 00 00 callq 40058c <_ZN1B7method2Ev> 40057b: b8 00 00 00 00 mov $0x0,%eax 400580: c9 leaveq 400581: c3 retq 

In this exact case, your object a/b's memory is in Stack. allocated by

400558: 48 83 ec 20 sub $0x20,%rsp 

as no member variable in your class, you class's instance will consume 1 byte (done by c++ compiler, for distinguish instances of the class)

you'll see the a's "this" pointer is transfered to _ZN1A7method1Ev (method1) by

 400563: 48 8d 45 ff lea -0x1(%rbp),%rax 400567: 48 89 c7 mov %rax,%rdi 

In this exact code case, you no need and can not free a's memory (on stack) before b.

  • if you mean memory allocated by A. by malloc/new . then you should write free/delete code in A's destructor. and put a to an scope wrapped by {}, when a out of the scope the destructor will be called (which is automatically done by c++ compiler). like @Alok and @Luchian mentioned.

  • if your memory is allocated in form of member variable, like following:

    class A{ char foo[16]; };

    when code is A a; then "foo" is allocated on stack as part of instance of A. you can not do anything.

    when code is A *pa = new A(); then object is allocated on A, feel free to delete it. delete pa;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.