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;