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.
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";
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[].
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.
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.
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).
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.
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;
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.
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.
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.
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,
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.
delete is the common keyword that frees allocated memory, whereas delete[] is the overloaded version of delete, overloaded to use delete on multidimensional arrays
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 [].
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[].
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