CoolInterview.com - World's Largest Collection of Interview Questions
 Interview Questions  
 Our Services  

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!


    Send your Resume to 6000 Companies


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

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

    Contact syed laikudeen.A  Contact syed laikudeen.A
    Category C++ Interview Questions
    Rating (1.0) By 1 users
    Added on 1/10/2008
    Views 3175
    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


    If you have the better answer, then send it to us. We will display your answer after the approval.
    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
    What are advantages of pure virtual function?
    View Answer
    When do we use abstract.h?Give an example with a small program.
    View Answer
    write a program on far,null,near,void ,wild,this pointers? And find out the advantages and disadvantages among these pointers?
    View Answer
    Write a program on abstract class.
    View Answer
    Is it possible to know that how much memory used by a member function or constructor?
    View Answer
    what are the differences between member function and constructor?
    View Answer
    why c++ is called as object-oriented language? Here what does object mean?
    View Answer
    what are the differences between c and c++.what are the advantages the are only in c++ but not in c
    View Answer
    Write a C++ program that contains a structure named ?Student? having two data members

    1) Name

    2) CGPA
    Declare array of structure ?Student? of size 10 .Populate this array by taking data from file inputFile.txt.File inputFile.txt contains Name and CGPA of the students for current semester. There is a single space between the Name and CGPA.

    Display the un-sorted list of students on screen. Compare the CGPAs of all the students and sort Student List with respect to CGPA in ascending or descending order.

    Display the sorted list of students, highest CGPA and lowest CGPA on screen and also write the result in File outputFile.txt with tab characters between the Name and CGPA.
    Sample InputFile.txt
    Ahmer 4.0

    Aasim 3.5
    Usman 3.4
    Maria 2.4
    Anila 3.0
    Humza 2.6
    Junaid 3.0
    Talaal 3.8
    Haris 3.9
    Saira 2.6
    Sample OutputFile.txt

    Sample outputFile.txt





    Name CGPA

    ===============

    Maria 2.4

    Humza 2.6

    Saira 2.6

    Anila 3.0

    Junaid 3.0

    Usman 3.4

    Aasim 3.5

    Talaal 3.8

    Haris 3.9

    Ahmer 4.0

    ===============

    Highest CGPA = 4.0

    Lowest CGPA = 2.4

































    Sample output of screen:



    Unsorted List of Students

    Name CGPA

    Ahmer 4.0

    Aasim 3.5

    Usman 3.4

    Maria 2.4

    Anila 3.0

    Humza 2.6

    Junaid 3.0

    Talaal 3.8

    Haris 3.9

    Saira 2.6



    Sorted List of Students

    Name CGPA

    ===============

    Maria 2.4

    Humza 2.6

    Saira 2.6

    Anila 3.0

    Junaid 3.0

    Usman 3.4

    Aasim 3.5

    Talaal 3.8

    Haris 3.9

    Ahmer 4.0

    ===============

    Highest CGPA = 4.0
    Lowest CGPA = 2.4
    View Answer
    how can we made digital calendar by using c++ class
    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL C++ Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2009 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/12719/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | Joke of the Day

    0.55