Mail Archives: djgpp/1997/05/28/11:30:03
Nathan Gray wrote:
>
> Since we're on the subject of C and C++, does anyone have a good
> explanation for the differences between malloc() and new? As a native
> C++ programmer, my first instinct is to use new, but I see malloc() so
> often I'm starting to wonder if I'm missing something.
Well, I am not a big C++ expert, as I usually prefer programming in C,
but this is what I think:
new calls the constructor of a class while malloc does not.
This means that if a class does not use any other constructor just the
default one, you can allocate memory for an instance of this class using
malloc (or calloc), and free it via free(), while if your class depends
on some constructor code to allocate some memory for it for instance,
then you must use new.
Andras
- Raw text -