debugging - C++ Heap/Stack. Why only one new-operator? -
i'm starting learn topic of dynamic memory allocation.
i have following code:
#include <iostream> #include "a.h" #include "b.h" using namespace std; int main() { /* both objects on stack */ classastack; b classbstack; /* both objects on heap*/ // *classaheap = new a(); // b *classbheap = new b(); /* objects on heap b ???*/ *classaheap = new a(); return 0; }
#ifndef a_h_ #define a_h_ #include <iostream> #include "b.h" class { public: a(); virtual ~a(); public: b b; }; #endif /* a_h_ */
#include "a.h" a::a() { std::cout <<"constructor called" << std::endl; } a::~a() { }
#ifndef b_h_ #define b_h_ #include <iostream> class b { public: b(); virtual ~b(); }; #endif /* b_h_ */
#include "b.h" b::b() { std::cout <<"constructor b called" << std::endl; } b::~b() { }
the output of debugger is:
temporary breakpoint 6, main () @ ../src/heapstacktest02.cpp:18 18 classastack; breakpoint 4, b::b (this=0x23aa58) @ ../src/b.cpp:12 12 std::cout <<"constructor b called" << std::endl; breakpoint 5, a::a (this=0x23aa50) @ ../src/a.cpp:13 13 std::cout <<"constructor called" << std::endl; breakpoint 4, b::b (this=0x23aa40) @ ../src/b.cpp:12 12 std::cout <<"constructor b called" << std::endl; breakpoint 4, b::b (this=0x60004b048) @ ../src/b.cpp:12 12 std::cout <<"constructor b called" << std::endl; breakpoint 5, a::a (this=0x60004b040) @ ../src/a.cpp:13 13 std::cout <<"constructor called" << std::endl; breakpoint 1, main () @ ../src/heapstacktest02.cpp:30 30 return 0;
to question:
where member-variable b
of class a
?
if @ address in section 0x23a, seems stack, , section 0x6000 seems heap.
i'm working on windows 64-bit system.
why member-variable b
on heap, without new
operator being called?
Comments
Post a Comment