The two Forms of ‘new’ and ‘delete’

3 07 2011

There are two forms of new and delete in C++. They are different depending on whether you are creating a single object or an array of objects. When creating a single object you use this form :

string *strPtr = new string;
delete strPtr;

For arrays you use the square bracket form of new and delete:

string *strPtr = new string[100];
delete [] strPtr;

You cannot mix and match these operations, using the single form of new with the array form of delete or vice versa is undefined.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: