6 Replies - 7515 Views - Last Post: 22 January 2008 - 03:35 AM Rate Topic: -----

#1 #include<wmx010>  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 75
  • Joined: 19-January 08

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

Posted 20 January 2008 - 08:18 AM

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).

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;




Is This A Good Question/Topic? 0
  • +

Replies To: What is the difference between 'delete' and 'delete[]'

#2 PennyBoki  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

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

Posted 20 January 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.
Was This Post Helpful? 0
  • +
  • -

#3 #include<wmx010>  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 75
  • Joined: 19-January 08

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

Posted 20 January 2008 - 08:48 PM

View PostPennyBoki, on 20 Jan, 2008 - 08:06 PM, said:

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 January 2008 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

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

Posted 20 January 2008 - 09:09 PM

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.
Was This Post Helpful? 0
  • +
  • -

#5 #include<wmx010>  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 75
  • Joined: 19-January 08

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

Posted 20 January 2008 - 09:25 PM

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. ;)
Was This Post Helpful? 0
  • +
  • -

#6 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 14
  • View blog
  • Posts: 1,120
  • Joined: 01-August 07

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

Posted 20 January 2008 - 09:35 PM

Your signature is too big.
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

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

Posted 22 January 2008 - 03:35 AM

View Post#include<wmx010>, on 21 Jan, 2008 - 03:48 AM, said:

View PostPennyBoki, on 20 Jan, 2008 - 08:06 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1