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 ?

Alok Save
  • 202,538
  • 53
  • 430
  • 533
user1838343
  • 451
  • 2
  • 8
  • 16

4 Answers4

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++.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
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();
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
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;
}
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
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;

whunmr
  • 2,435
  • 2
  • 22
  • 35