CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Interview Success Kit - 9,000 Interview Que. & Ans.
Covers 9,000+ Interview Questions asked in actual MNC Interviews - all with model answers.
Get Your Dream Job Now - www.vyoms.com
Sponsored Links
Interview Questions
Our Services

INTERVIEW QUESTIONS C++ FUNCTIONS IN C++ DETAILS
Question :
What is the difference between delete and delete [ ]?

Posted by: syed laikudeen.A on 1/10/2013

Contact syed laikudeen.A Contact syed laikudeen.A
Category Functions in C++ Interview Questions
Rating (3.7) By 58 users
Added on 1/10/2013
Views 3484
Rate it!
Answers:

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.



Posted by: eswar

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.



Posted by: BALAN K

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";



Posted by: g.i.o.e

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[].



Posted by: sivakumargayam

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.



Posted by: venu

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.



Posted by: c++prof

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.



Posted by: Girish

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



Posted by: hemant kumar Joshi

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.



Posted by: Muhammad Ajmal

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;



Posted by: pradeepa

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.



Posted by: vidhya

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.



Posted by: Hepsiba Sushma

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.



Posted by: Dheeraj Sharma

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.



Posted by: saritha

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[].



Posted by: suresh

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.



Posted by: Rajashekhar N

Contact Rajashekhar N Contact Rajashekhar N

Delete will delete only single object, and delete[] will delete array of objects.



Posted by: Radha

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,



Posted by: Amaren

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.



Posted by: Puja Nanda

Contact Puja Nanda Contact Puja Nanda

delete is used to deallocate single memory while delete[] is used for deallocate array of memory.



Posted by: paresh patel

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[].



Posted by: Manjunath

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[].



Posted by: Urvashi Gautam

Contact Urvashi Gautam Contact Urvashi Gautam

Delete just deletes the pointer to an object

Delete[] deletes an array of pointers



Posted by: K V Sunil

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



Posted by: prastha saxena

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 [].



Posted by: Harish kukreja

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.



Posted by: Rajeev Unnithan

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[].



Posted by: manjula

Contact manjula Contact manjula

deleteis a keyword & delete[] is a function



Posted by: priyanka thomas

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.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification Enter the above shown code: *
    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?

    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

    User Options
    Latest 20 Questions
    What are the different type of networking / internetworking devices?
    What is Protocol Data Unit?
    What are the types of Transmission media?
    What do you meant by "triple X" in Networks?
    What Automatic Repeat Request (ARQ)?
    What are the Data link protocols?
    What are the categories of Transmission media?
    what is the maximum segment length of a 100Base-FX network?
    What is one basic requirement for establishing VLANs?
    What is the role of IEEE in computer networking?
    What is the importance of Encryption on a network?
    What is the use of a default gateway?
    What are gateways?
    What are firewalls?
    How can you identify the IP class of a given IP address?
    What is OSI and what role does it play in computer networks?
    What does 10Base-T mean?
    What are proxy servers and how do they protect computer networks?
    What is the importance of the OSI Physical Layer?
    How does a network topology affect your decision in setting up a network?


    Copyright ©2003-2016 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

    Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes


    Cache = 0 Seconds