Code:#include <iostream> class foo{ void *a; public: foo(){ a = malloc(10); std::cout << "baz" << std::endl; } foo(int size){ a = malloc(size); std::cout << "bar" << std::endl; } ~foo(){ free(a); } }; void bar() { foo x(5); } void baz() { foo *y = new foo; delete(y); } int main() { bar(); baz(); return 0; }
I presume that the destructor in this case will destroy x when we are leaving the scope of bar, eventhough it's allocated on the heap, but what happens with y in baz where I use new? The constructor presumably takes care of the allocation, is the whole object a bit larger that the malloced memory in this case?
Is there something else besides malloc I can use here?