CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: What is the difference between delete and delete [ ]?

Answer: The keyword delete is used to destroy the single variable memory created dynamically which is pointed by single pointer variable.
Eg: int *r=new(int)
the memory pointed by r can be deleted by delete r.
delete [] is used to destroy array of memory pointed by single pointer variable.
Eg:int *r=new(int a[10])

The memory pointed by r can be deleted by delete []r.



Category Functions in C++ Interview Questions & Answers - Exam Mode / Learning Mode
Rating (3.7) By 58 users
Added on 1/10/2013
Views 3550
Rate it!
Question: What is the difference between delete and delete [ ]?
Answer:

The keyword delete is used to destroy the single variable memory created dynamically which is pointed by single pointer variable.
Eg: int *r=new(int)
the memory pointed by r can be deleted by delete r.
delete [] is used to destroy array of memory pointed by single pointer variable.
Eg:int *r=new(int a[10])

The memory pointed by r can be deleted by delete []r. Source: CoolInterview.com

Answered by: eswar | Date: 2/12/2008 | Contact eswar Contact eswar

If "a" is an array of objects then ,

delete a --> Destructor routine is called only for the first object in the array and the entire array is de-allocated from memory.

delete [] a --> Destructor routine is called for each of the objects in the array and subsequently the complete array is de-allocated from memory. Source: CoolInterview.com

Answered by: BALAN K | Date: 2/12/2008 | Contact BALAN K Contact BALAN K

delete operator is used to delete ordinary variable which has been created dynamically pointed by pointer variable.
e.g: int *p=new int a;
This memory can be deleted by using "delete p";
delete[] is used to delete array variable which has been created dynamically pointed by single pointer variable.
eg:int *p=new int a[10];
this memory can be deallocated by "delete []p"; Source: CoolInterview.com

Answered by: g.i.o.e | Date: 2/12/2008 | Contact g.i.o.e Contact g.i.o.e

The delete operator destroys the object created with new by deallocating the memory associated with the object. The delete[] operator free storage allocated for array objects created with new[]. Source: CoolInterview.com

Answered by: sivakumargayam | Date: 2/16/2008 | Contact sivakumargayam Contact sivakumargayam

When you allocate a memory to an array using :
char *buff = new char[BuffLength]
..
..
You need to use delete []buff.
Now consider a case where you have allocated memory to an integer var, as below :
int iNum = new int;
Then after processing you can use
delete iNum. Source: CoolInterview.com

Answered by: venu | Date: 2/19/2008 | Contact venu Contact venu

delete is used when you want to delete single variable or a single object which is created at runtime and delete[] is used to specify that you want to delete an entire array which was created at runtime. Source: CoolInterview.com

Answered by: c++prof | Date: 2/22/2008 | Contact c++prof Contact c++prof

Look at this sample code

int* pI = new int; //allocates 2 bytes
int *pJ = new int[30];//allocates memory for storing 30 integers
//Do stuff
delete pI;
delete[] pJ

We can see that delete[] "deletes" not a single "thing" but an array of things whereas delete just deletes a single entity.
Source: CoolInterview.com

Answered by: Girish | Date: 2/23/2008 | Contact Girish Contact Girish

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). Source: CoolInterview.com

Answered by: hemant kumar Joshi | Date: 2/26/2008 | Contact hemant kumar Joshi Contact hemant kumar Joshi

delete operator is used to delete a single object which we have created using the new operator like this
//create object
Object obj = new Object();
//delete the object
delete obj;
and delete[] is used to delete all the objects in an array using the new operator like this
//create array of objects
Object obj[] = {new Object(),new Object()};
//delete the array of objects
delete[] obj;

Basically when we use delete operator with some object, it perform two action first it execute its destructor and then destroy it from memory, we use delete[] operator with array of object to call the destructor of all array objects.

Source: CoolInterview.com

Answered by: Muhammad Ajmal | Date: 2/27/2008 | Contact Muhammad Ajmal Contact Muhammad Ajmal

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:
// allocates a single integer
int *pi = new int;
// allocates an array of 10
int *pi_array = new int[10]; delete pi; pi = 0;
delete [] pi_array;
pi_array = 0;

Source: CoolInterview.com

Answered by: pradeepa | Date: 2/27/2008 | Contact pradeepa Contact pradeepa

"delete p" will free the space allocated by the pointer p,
"delete[] p" will free the all the space allocated by array of pointer p. Source: CoolInterview.com

Answered by: vidhya | Date: 2/29/2008 | Contact vidhya Contact vidhya

There are same when a normal variable is deleted but when an array is deleted "delete" deletes the first element of array where as "delete []" deletes the complete array. Source: CoolInterview.com

Answered by: Hepsiba Sushma | Date: 3/4/2008 | Contact Hepsiba Sushma Contact Hepsiba Sushma

When we use only delete it does not delete whole dynamically allocated memory for array it only delete first memory allocation which contain base address of array but in delete[] it delete whole memory allocation. Source: CoolInterview.com

Answered by: Dheeraj Sharma | Date: 3/7/2008 | Contact Dheeraj Sharma Contact Dheeraj Sharma

delete is used for deleting the memory allocated for single element.
delete[] is used for deleting the memory allocated for array of elements. Source: CoolInterview.com

Answered by: saritha | Date: 3/17/2008 | Contact saritha Contact saritha

Delete is used to deallocate the memory allocated by new.

Delete[] is used to deallocate the group of memory allocate by new[]. Source: CoolInterview.com

Answered by: suresh | Date: 3/25/2008 | Contact suresh Contact suresh

delete command deletes the object created by the new command so that it free the memory allocated to that particular object.
delete [] command does same as that of above delete command. However the difference is it deletes an array of objects created by new [] command so that it free the memory allocated to an array of objects. Source: CoolInterview.com

Answered by: Rajashekhar N | Date: 3/28/2008 | Contact Rajashekhar N Contact Rajashekhar N

Delete will delete only single object, and delete[] will delete array of objects. Source: CoolInterview.com

Answered by: Radha | Date: 4/1/2008 | Contact Radha Contact Radha

If u have created as array of some data type using NEW ie in the heap then we should use
delete [] <arrayname>
which deletes the complete array.
else if we use
delete <arrayname>
only the first element will be deleted and others will still be there in the heap. These will lead to memory leak,
Source: CoolInterview.com

Answered by: Amaren | Date: 4/2/2008 | Contact Amaren Contact Amaren

When v talk bout delete keyword, we intend to delete just an object in focus.
On the other hand delete[] is required when ve intend to delete an array of objects. Source: CoolInterview.com

Answered by: Puja Nanda | Date: 4/3/2008 | Contact Puja Nanda Contact Puja Nanda

delete is used to deallocate single memory while delete[] is used for deallocate array of memory. Source: CoolInterview.com

Answered by: paresh patel | Date: 4/9/2008 | Contact paresh patel Contact paresh patel

If you want to delete one element you have to use delete, And suppose if you have to delete array of elements then you have to use delete[]. Source: CoolInterview.com

Answered by: Manjunath | Date: 4/9/2008 | Contact Manjunath Contact Manjunath

Delete is used to destroy object created using new for deallocation of memory,whereas delete[] is used to destroy array of object created by new[]. Source: CoolInterview.com

Answered by: Urvashi Gautam | Date: 4/12/2008 | Contact Urvashi Gautam Contact Urvashi Gautam

Delete just deletes the pointer to an object

Delete[] deletes an array of pointers Source: CoolInterview.com

Answered by: K V Sunil | Date: 4/18/2008 | Contact K V Sunil Contact K V Sunil

delete is the common keyword that frees allocated memory, whereas delete[] is the overloaded version of delete, overloaded to use delete on multidimensional arrays Source: CoolInterview.com

Answered by: prastha saxena | Date: 4/23/2008 | Contact prastha saxena Contact prastha saxena

The use of square brackets to indicate that what we are deleting is an array. When removing arrays from the free store, you should always include the square brackets or the results will be unpredictable. Note also that you do not specify any dimensions here, simply [].
Source: CoolInterview.com

Answered by: Harish kukreja | Date: 4/25/2008 | Contact Harish kukreja Contact Harish kukreja

delete is used to delete a single memory allocation, but delete [] is used for deleting an array of memory. Source: CoolInterview.com

Answered by: Rajeev Unnithan | Date: 4/30/2008 | Contact Rajeev Unnithan Contact Rajeev Unnithan

delete is used to delete a single object that is already allocated using the new operator whereas delete[] is used to delete an array of objects that is already allocated by the new[]. Source: CoolInterview.com

Answered by: manjula | Date: 5/16/2008 | Contact manjula Contact manjula

deleteis a keyword & delete[] is a function Source: CoolInterview.com

Answered by: priyanka thomas | Date: 5/21/2010 | Contact priyanka thomas Contact priyanka thomas


If you have the better answer, then send it to us. We will display your answer after the approval

Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
Is it possible to know that how much memory used by a member function or constructor?
View Answer
What is void main mean?
View Answer
what is abstractor in c++?
View Answer
I want a program on INLINE function and demonstrate the program with INLINE function and without INLINE function.Because i want to know clearly the difference between INLINE FUNCTION AND FUNCTION
View Answer
What do you mean by inline function?


View Answer
Difference between realloc() and free()?


View Answer
What are the types of STL containers?

View Answer
How to write a program such that it will delete itself after execution?




View Answer
What are inline functions?

View Answer
What is the difference between macro and inline()?

View Answer
Can we take "main function" as of type float,char etc?


- Functions in C++ Interview Questions & Answers" class="btn btn-primary btn-xs">View Answer
What is the difference between Function and Member function?



View Answer
What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)?
View Answer
Does Console.WriteLine() stop printing when it reaches a NULL character within a string?
View Answer
What does the keyword virtual mean in the method definition?
View Answer
What is public, protected, private?
View Answer
What is friend function?
View Answer
What do you mean by inline function?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Functions in C++ Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
"Holiday Packages" announced as an advertisement are an example of _________: (a) Offer (b) Counter Offer (c) Invitation to Offer (d) None of the above
A match fixing contract between a player and a broker is a: (a) Valid Contract (b) Unenforceable Contract (c) Void Contract (d) Illegal Contract
When a bookseller sells a book on cash payment then it is called as ___________: (a) Unilateral Contract (b) Bilateral Contract (c) Executed Contract (d) Executory Contract
____________ agreements are created by situation: (a) Written (b) Oral (c) Void (d) Implied
Cache = 0.015625 Seconds