Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,473 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,910 people online right now. Registration is fast and FREE... Join Now!




What is the difference between 'delete' and 'delete[]'

 
Reply to this topicStart new topic

What is the difference between 'delete' and 'delete[]'

#include<wmx010>
20 Jan, 2008 - 07:18 AM
Post #1

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
Q: What is the difference between 'delete' and 'delete[]'?

A: Whenever you allocate memory with 'new[]', you have to free the memory using 'delete[]'. When you allocate memory with 'new', then use 'delete' without the brackets. You use 'new[]' to allocate an array of values (always starting at the index 0).

CODE


int *pi = new int; // allocates a single integer
int *pi_array = new int[10]; // allocates an array of 10 integers

delete pi;
pi = 0;

delete [] pi_array;
pi_array = 0;


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: What Is The Difference Between 'delete' And 'delete[]'
20 Jan, 2008 - 07:06 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
What is the purpose of this post?

Is it answering a question?
If so, then it is nice of you to help and contribute, but I suggest you write a tutorial on some subject or a snippet. I don't think anyone would look for an answer in a thread with no replies.
User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: What Is The Difference Between 'delete' And 'delete[]'
20 Jan, 2008 - 07:48 PM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
QUOTE(PennyBoki @ 20 Jan, 2008 - 08:06 PM) *

What is the purpose of this post?

Is it answering a question?
If so, then it is nice of you to help and contribute, but I suggest you write a tutorial on some subject or a snippet. I don't think anyone would look for an answer in a thread with no replies.


Actually i posted this in the C++ TUTORIALS section. The MODS must have confused it with a Question and moved it over the Help section.



This post has been edited by #include<wmx010>: 20 Jan, 2008 - 07:49 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: What Is The Difference Between 'delete' And 'delete[]'
20 Jan, 2008 - 08:09 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,301



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
It was in the tutorials, but if you check your email you will see why I moved it here. It does not meet the guidelines as you submitted it for a tutorial.
User is online!Profile CardPM
+Quote Post

#include<wmx010>
RE: What Is The Difference Between 'delete' And 'delete[]'
20 Jan, 2008 - 08:25 PM
Post #5

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
That's weird i didn't receive any email. Anyway. you can delete the thread, there's no point leaving it here as it's not a question. wink2.gif
User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: What Is The Difference Between 'delete' And 'delete[]'
20 Jan, 2008 - 08:35 PM
Post #6

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 994



Thanked: 4 times
Dream Kudos: 375
My Contributions
Your signature is too big.
User is offlineProfile CardPM
+Quote Post

Bench
RE: What Is The Difference Between 'delete' And 'delete[]'
22 Jan, 2008 - 02:35 AM
Post #7

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 683



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(#include<wmx010> @ 21 Jan, 2008 - 03:48 AM) *

QUOTE(PennyBoki @ 20 Jan, 2008 - 08:06 PM) *

What is the purpose of this post?

Is it answering a question?
If so, then it is nice of you to help and contribute, but I suggest you write a tutorial on some subject or a snippet. I don't think anyone would look for an answer in a thread with no replies.


Actually i posted this in the C++ TUTORIALS section. The MODS must have confused it with a Question and moved it over the Help section.

You could do with expanding on your answer - in particular, you might explain the difference between the behaviour of these two instructions, to make it clear exactly why delete[] is required for an array. Specifically, you might explain how delete only deallocates a single element. delete and delete[] also call a destructor for non-POD types, which is an added sting for arrays of types with a non-trivial destructor - since these objects will be left intact in memory, the behaviour that a program may expect from the destruction of these objects will never happen.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 02:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month