-1

Why constructor/destructor function is not called while using malloc and free in c++ instead when we use new and delete , the constructor/destructor is called? why is it so?

2 Answers2

5

malloc and free are purely memory-management functions, they don't know anything about classes (and existed long before C++). They're low-level memory manipulation.

C++ added classes to C, and as part of that process, added new and delete to create and destroy instances of classes. That's different from low-level memory management.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • If an object is not created as you said, then why does this code works: class Test { public: Test() { cout << "Test : ctor\r\n"; } ~Test() { cout << "Test : dtor\r\n"; } void Hello() { cout << "Test : Hello World\r\n"; } }; int main() { cout << "2\n"; Test* t2 = (Test*) malloc(sizeof Test); t2->Hello(); free(t2); return 0; }" @T.J. Crowder – Sarah Irfan Mar 19 '15 at 16:59
  • @SarahIrfan: Because the mechanics of non-virtual method calls let the compiler resolve the method address correctly without the object being correctly initialized. Doesn't mean you should rely on it, nor does it mean that it's right. Bottom-line: If you want to correctly *create an object*, use `new`. – T.J. Crowder Mar 19 '15 at 17:11
4

malloc is a C function that pre-dates constructors. free is a C function that pre-dates destructors. They are both C functions that must operate in a language without constructors and destructors.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • but when we use malloc (for a class) a new object of a class is created , so it must execute all the functions in the class including constructor and destructoor. – Sarah Irfan Mar 18 '15 at 16:29
  • 2
    @SarahIrfan: *"when we use malloc (for a class) a new object of a class is created"* No, it isn't. Memory of a relevant size may be allocated, but an object has not been created. – T.J. Crowder Mar 18 '15 at 16:30
  • 1
    @SarahIrfan No, a new object *is not* created. That is the point. To be created, it's constructor must be called. – juanchopanza Mar 18 '15 at 16:30
  • @SarahIrfan Not it "must" not. It "must" do what the standard says malloc shall do. – Daniel Daranas Mar 18 '15 at 16:32
  • @juanchopanza But when we create a pointer to a class, and point it to a memory of the required size, does that not mean we have in fact created the object of that class? malloc(sizeof(class_name)) returns address of the memory allocated, but when we assign this memory to the pointer of the class, it should mean an object has been created. How do you define 'creating an object'? – Sarah Irfan Mar 19 '15 at 15:21
  • @SarahIrfan No. It doesn't. Creating an object is allocating memory for it and calling its constructor. – juanchopanza Mar 19 '15 at 15:23
  • @juanchopanza ok then how do you define creating an object? – Sarah Irfan Mar 19 '15 at 15:24
  • @juanchopanzaIf an object is not created as you said, then why does this code works: class Test { public: Test() { cout << "Test : ctor\r\n"; } ~Test() { cout << "Test : dtor\r\n"; } void Hello() { cout << "Test : Hello World\r\n"; } }; int main() { cout << "2\n"; Test* t2 = (Test*) malloc(sizeof Test); t2->Hello(); free(t2); return 0; }" – Sarah Irfan Mar 19 '15 at 16:56
  • @SarahIrfan It doesn't work. It doesn't create the object. Please go read a book or something, I am tired of repeating the same thing. – juanchopanza Mar 19 '15 at 18:16