   What new does that malloc doesn't ?
  Hi! Maybe I should have posted this in for beginners...Are the following pices of code functionnaly identical ?class MyClass{    MyClass() { Init(); }    void Init(); // does some stuff    // Other vars etc.}int main(){    MyClass* c = new MyClass();}And the second piece:class MyClass{    void Init(); // does some stuff    // Other vars etc.}int main(){    MyClass* c = (MyClass*) malloc( sizeof(MyClass) );    c->Init();}PS: if I messed up the malloc parameters please ignore it and assume I correctly allocated a blockIn other words, does a piece of code gains/loses anything particular being executed from within a constructor ?Edit: changed topic name to something less 'polemical'CheersJanta[Edited by - janta on June 18, 2006 1:36:54 AM]
  Cleaner code?2 operations (using new) instead of 4 (using malloc)?   ;Quote:Original post by jantaAre the following pices of code functionnaly identical ?No.  If your class contains any virtual functions, the malloc version will not properly initialize the virtual table - placement new must also be called (manually) for that to occur.  If any of your variables are references, they can only be initialized from a constructor initializer list, again requiring placement new.  Since free() and delete are not interchangable, the malloc version cannot be used with many smart pointers (which would plug the memory leak in main() you have).  Since initializing from the constructor is the defacto method of initializing in C++, if any other programmers need to work with your code, they will need to find out you're doing things differently (wasting time).   ;  this discussion is about *functionnal similarity* only, please :) I would never use the second form in my code (if someone would, I'd like to know the reasons of course)@MaulingMonkey > ah ! good point, I hadn't thought of the  virtual function tables. And regarding delete/free, I assumed that was obvious.Thanks for reply!   ;  LOLWell for one malloc, if it fails, would return a null pointer.new would never return a null pointer, but it could throw a std::bad_alloc.   ;  Thing new does/is that malloc doesnt/isn't:- Set up the virtual function table- Throw an exception in case of failure instead of returning new- initializes references form the initializer list.- can be overloaded (redefined)- Is type-safe: returns a pointer that doesnt need casting.- pairs-up with delete (malloc() pairs up with free())I will edit that list as more differences are mentionned.PS: this reminds me of an old Sega advertisement: Genesis does what nintendon't ;) Anyone remembers it ?[Edited by - janta on June 18, 2006 2:35:21 AM];  A couple more:The behavior of new can be changed by overloading operator new, malloc has no equivilant.New is typesafe, malloc isn't.   ;Quote:Original post by MaulingMonkeyThe behavior of new can be changed by overloading operator new, malloc has no equivilant.operator new() can be overloaded on a class-by-class basis.new can be made to return 0 on failure with Foo* ptr = new (nothrow) Foo; if you don't like exceptions.You can actually control what happens (e.g. garbage collection) when operator new() fails to allocate memory by using std::set_new_handler(). new[] will default-construct all the elements of an array.new Foo; will not value-initialize POD-types (like malloc()), but new Foo() will.Having an Init() function is not the same thing as having a constructor: the constructor can directly initialize the class members, while the Init() function can only assign them. That's important when some of the members are references or const.   ;Quote:Original post by Frunynew Foo; will not value-initialize POD-types (like malloc()), but new Foo() will.Just wondering, is there a reason for this different behaviour?   ;Quote:Original post by sirGustavJust wondering, is there a reason for this different behaviour?Hmmm, because the language specification says so? [smile]POD-types are normally not initialized when constructed (just like in C) but you can force the value-initialization (basically, zeroing out the memory) that way.   