C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Prefer new and delete operators over malloc and free


Posted Date: 19 Jun 2008    Resource Type: Tips    Category: General

Posted By: Lekshmi m nair       Member Level: Silver
Rating:     Points: 5



Consider the following two ways to get space for an array of 10 string objects, one using malloc, the other using new:


string *stringArray1 = static_cast(malloc(10 * sizeof(string)));
string *stringArray2 = new string[10];


Here stringArray1 points to enough memory for 10 string objects, but no objects have been constructed in that memory. Furthermore, you have no way to initialize the objects in the array.

In contrast, stringArray2 points to an array of 10 fully constructed string objects, each of which can safely be used in any operation taking a string. The call to free will release the memory pointed to by stringArray1, but no destructors will be called on the string objects in that memory.

If the string objects themselves allocated memory, as string objects are wont to do, all the memory they allocated will be lost. On the other hand, when delete is called on stringArray2, a destructor is called for each object in the array before any memory is released.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Prefer new and delete operators over malloc and free  .  Prefer new and delete operators  .  New and delete operators  .  Advantages of new operator over malloc  .  Advantages of new and delete operators  .  Advantages of delete operator over free  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Providing dfifferent accessibility for get/set of properties
Previous Resource: Firefox 3 Shortcuts
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use