Also - new and delete are typesafe, hence better. (malloc returns void*) - new gives you the feature of "placement new", which allows you create an object in a pre-determined location, which is helpful I think in device drivers programming...
-- On Tue, Jul 12, 2011 at 10:39 AM, sagar pareek <[email protected]>wrote: > new and delete operator calls condtructors and destructors respectively :) > > On Wed, Jul 6, 2011 at 6:23 PM, Navneet Gupta <[email protected]>wrote: > >> The basic reason is graduation. >> >> Since C++ is object oriented and designers wanted to provide more >> safety in case of dynamic memory allocations, they defined new and >> delete operators to handle allocation and deallocation. >> >> If you see malloc, if does only one function - allocate the memory, >> with new operator on an object, it's appropriate constuctor will be >> called and you are guaranteed with proper initialization specially >> when we use standard libraries. So new faciliates allocation as well >> as initialization, similarly delete faciliates deallocation as well as >> destruction (clean up code inside destructor function). >> >> With free, user won't be having any handle on object if allocated with >> new until it is allocated memory and appropriately initialized. >> >> Also, AFAIK, chances of getting proper memory allocation are more with >> new than malloc. Somebody on the group may confirm. >> >> On Wed, Jul 6, 2011 at 6:07 PM, Tamanna Afroze <[email protected]> >> wrote: >> > I think C++ has some advanced feature for memory allocation like new. >> > >> > On Wed, Jul 6, 2011 at 6:34 PM, Piyush Sinha <[email protected]> >> > wrote: >> >> >> >> Why is it suggested not to use malloc() or calloc() in C++ for memory >> >> allocation? >> >> >> >> >> >> -- >> >> *Piyush Sinha* >> >> *IIIT, Allahabad* >> >> *+91-8792136657* >> >> *+91-7483122727* >> >> *https://www.facebook.com/profile.php?id=100000655377926 * >> >> >> >> -- >> >> You received this message because you are subscribed to the Google >> Groups >> >> "Algorithm Geeks" group. >> >> To post to this group, send email to [email protected]. >> >> To unsubscribe from this group, send email to >> >> [email protected]. >> >> For more options, visit this group at >> >> http://groups.google.com/group/algogeeks?hl=en. >> >> >> > >> > >> > Regards, >> > Tamanna >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups >> > "Algorithm Geeks" group. >> > To post to this group, send email to [email protected]. >> > To unsubscribe from this group, send email to >> > [email protected]. >> > For more options, visit this group at >> > http://groups.google.com/group/algogeeks?hl=en. >> > >> >> >> >> -- >> Navneet >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > > > -- > **Regards > SAGAR PAREEK > COMPUTER SCIENCE AND ENGINEERING > NIT ALLAHABAD > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
