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 - Get Success in Job Interviews Interview Success Kit - 30,000 Interview Que. & Ans.
Covers 30,000+ Interview Questions asked in actual MNC Interviews - all with model answers.

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 3510
Rate it!


Answers:

Source: CoolInterview.com

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



Posted by: eswar

Contact eswar Contact eswar

Source: CoolInterview.com

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



Posted by: BALAN K

Contact BALAN K Contact BALAN K

Source: CoolInterview.com

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



Posted by: g.i.o.e

Contact g.i.o.e Contact g.i.o.e

Source: CoolInterview.com

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



Posted by: sivakumargayam

Contact sivakumargayam Contact sivakumargayam

Source: CoolInterview.com

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



Posted by: venu

Contact venu Contact venu

Source: CoolInterview.com

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



Posted by: c++prof

Contact c++prof Contact c++prof

Source: CoolInterview.com

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



Posted by: Girish

Contact Girish Contact Girish

Source: CoolInterview.com

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



Posted by: hemant kumar Joshi

Contact hemant kumar Joshi Contact hemant kumar Joshi

Source: CoolInterview.com

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



Posted by: Muhammad Ajmal

Contact Muhammad Ajmal Contact Muhammad Ajmal

Source: CoolInterview.com

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



Posted by: pradeepa

Contact pradeepa Contact pradeepa

Source: CoolInterview.com

"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



Posted by: vidhya

Contact vidhya Contact vidhya

Source: CoolInterview.com

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



Posted by: Hepsiba Sushma

Contact Hepsiba Sushma Contact Hepsiba Sushma

Source: CoolInterview.com

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



Posted by: Dheeraj Sharma

Contact Dheeraj Sharma Contact Dheeraj Sharma

Source: CoolInterview.com

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



Posted by: saritha

Contact saritha Contact saritha

Source: CoolInterview.com

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



Posted by: suresh

Contact suresh Contact suresh

Source: CoolInterview.com

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



Posted by: Rajashekhar N

Contact Rajashekhar N Contact Rajashekhar N

Source: CoolInterview.com

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



Posted by: Radha

Contact Radha Contact Radha

Source: CoolInterview.com

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



Posted by: Amaren

Contact Amaren Contact Amaren

Source: CoolInterview.com

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



Posted by: Puja Nanda

Contact Puja Nanda Contact Puja Nanda

Source: CoolInterview.com

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



Posted by: paresh patel

Contact paresh patel Contact paresh patel

Source: CoolInterview.com

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



Posted by: Manjunath

Contact Manjunath Contact Manjunath

Source: CoolInterview.com

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



Posted by: Urvashi Gautam

Contact Urvashi Gautam Contact Urvashi Gautam

Source: CoolInterview.com

Delete just deletes the pointer to an object

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



Posted by: K V Sunil

Contact K V Sunil Contact K V Sunil

Source: CoolInterview.com

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



Posted by: prastha saxena

Contact prastha saxena Contact prastha saxena

Source: CoolInterview.com

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



Posted by: Harish kukreja

Contact Harish kukreja Contact Harish kukreja

Source: CoolInterview.com

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



Posted by: Rajeev Unnithan

Contact Rajeev Unnithan Contact Rajeev Unnithan

Source: CoolInterview.com

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



Posted by: manjula

Contact manjula Contact manjula

Source: CoolInterview.com

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



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
    India News Network

    Latest 20 Questions
    What is the RESTeasy framework?
    What is the Jersey framework?
    What is the Restlet framework?
    What is WADL?
    What is the difference between SOAP and other remote access techniques?
    What is the wsgen tool?
    What is the wsimport tool?
    What is encoding rules for header entries?
    What are 2 styles web service?s endpoint by using JAX-WS?
    What does SOAP encodingStyle attribute defines?
    What is the SOAP encoding?
    What is a SOAP envelope element?
    What is XOP?
    What is MTOM?
    What is JAXB?
    What is JAX-WS?
    What is WSDL?
    How to decide which one of web service to use REST or SOAP?
    What is the difference between a REST web service and a SOAP web service?
    What is REST?



    Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

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

    Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

    MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
    MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

    GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

    IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
    IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

    SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
    SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |




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



    Cache = 4.023438 Seconds