0

Why the ctor and dtor are not getting invoked even though the memory is allocated or freed? What is actually happening here? Please share your thoughts.

#include<iostream>
#include<stdlib.h>
using namespace std;

class a{
        public:
            int i;
                a() {cout<<"\n a ctor \n";}
                ~a(){cout<<"\n a dtor \n";}
};

main() {
        a *ap = NULL;
        ap = (a*)malloc(sizeof(a));
        ap->i = 11;
        cout<<ap->i<<"\n";
        cout<<ap<<"\n";
        free(ap); //does this actually work? Does this free the memory?
        cout<<ap<<"\n";
        ap = NULL; 
        cout<<ap;
}

does the above mean ctor and dtor are not useful or they are just useless?

Renuka
  • 217
  • 1
  • 6
  • 1
    At what point would you expect the ctor to be called? Inside `malloc`? (That seems unlikely, given that `malloc` is a C function, and has no information on types...) – Oliver Charlesworth Mar 23 '14 at 12:14
  • 1
    The constructor and destructor are not called, because `malloc`&`free` are not aware of constructors/destructors. That's it. Also, stop using `malloc`/`free`. `make_unique` and `make_shared` all the way. – milleniumbug Mar 23 '14 at 12:14
  • `does the above mean ctor and dtor are not useful or they are just useless?` No, it just means that `malloc`&`free` are useless and not useful. – milleniumbug Mar 23 '14 at 12:37
  • have you read http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free ? – wonko realtime Mar 23 '14 at 12:41

3 Answers3

1

Everything is ok here.
The constructor/destructor are just normal functions inside.
What malloc does: Reserve xy bytes of memory.
What new does: Call malloc (or something like that), then call the constructor.
Malloc shouldn´t call any constructor (and it can´t,
because it doesn´t know which one. It does know only a byte count).

If you want to handle memory stuff manually and then just call the constructor,
see "placement new"

deviantfan
  • 11,268
  • 3
  • 32
  • 49
0

There's nothing in malloc to trigger calling your descriptor; malloc is not for allocating objects it's for allocating a buffer for general use (and not normally for c++). Since malloc is a C library and knows nothing of C++, it would seem a bit off for it to consider calling a C++ constructor -- especially considering constructors can have arguments and malloc has no way to receive those.

If you have a valid reason to use malloc to allocate what will become an object, you are still responsible for ensuring the constructor and destructor get called. You do that with new and delete but your new call is modified to be a "placement new". it's extremely rare that you will have a legitimate use for this in conjunction with malloc, but its use in your example would be:

    void *ap_addr = void*)malloc(sizeof(a));
    ap = new(ap_addr)a();
    ap->i = 11;

Note that you are now responsible for calling both delete (to get the destructor called), and free() (to release the buffer). Of course, buffer release is optional if you're going to reuse it, for example.

mah
  • 39,056
  • 9
  • 76
  • 93
  • Wouldn't you call the destructor directly rather than using `delete` in this case? Since IIRC there isn't a placement delete. – James M Mar 23 '14 at 13:26
  • Actually since there's no guarantee that `new` uses `malloc`, calling `delete` here could be undefined behaviour. – James M Mar 23 '14 at 13:34
-1
  1. It should be int main
  2. What is the problem of using new>
  3. Do not mix new/malloc with delete/free
  4. use nullptr not NULL
Ed Heal
  • 59,252
  • 17
  • 87
  • 127