You can only delete what you have created with new. You cannot delete parts of an array. You either have to delete the whole array or don't delete the array at all.
delete[] data; // deletes whole array
You might want to shift all the elements after the removed element one position (or you can just overwrite the removed element with the last element if the order doesn't matter) and decrease totalLenghtArr by one.
In situations like this most people would just use std::vector which handles most of the tricky details for you.
What if I have an array let say [1,2,3,4,5] and It's a dynamic array. How can I free the memory of the last element. Are there ways to free the memory of an array's element.
delete does not delete anything. It is very badly named.
Don't see how it is badly named. It deletes the object that the pointer points to. Would you have preferred a name that reflected that? delete_pointee ptr; ?
delete means "an object or array that was allocated using new, that this pointer is pointing at - call the destructor and I don't care what happens to the memory after that". But when people start programming in C++, they see delete and they think it means remove from existence, because that's what the word "delete" actually means.
After calling delete, on a pointer, the pointer still exists. I can still use the pointer.
After calling delete, the memory occupied by what the pointer was pointing at is probably still mostly full of what was there before. What has been "deleted"? What no longer exists?
Other languages use words like "release" or "dealloc". Those are much better names.
mausmani2494 clearly thought that delete could be used to delete things, in the common meaning of the word "delete", which is incorrect and caused him difficulties. If delete was better named, he would not have thought that it could be used to remove objects from existence.
After calling delete, the memory occupied by what the pointer was pointing at is probably still mostly full of what was there before. What has been "deleted"? What no longer exists?
The object no longer exist. It has been deleted. That the memory is still there, untouched, is just an implementation detail and there is no way you can find out without wandering into undefined behaviour land.
You can call delete on elements of dynamic array only if it is array of pointers or even Matrix of pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13
int ** array=new array*[Capacity];
for(int i=0;i<length;i++)
array[i]=nullptr;//Just for the example
.
.
.
//if you delete an element in the array you must rotate elements to fill the gap
//for example
delete array[i];//
for (int i=i+1;i<length;i++)
array[i]=array[i+1];
--length;
Little did he know that the destructor of MyObject contains code to calculate the answer the Ultimate Question of Life, the Universe, and Everything.
Are there ways to free the memory of an array's element.
If you have a big box but you have given out some of your things so that the box is no longer full, you can ...
get a smaller box, move your remaining things from large box to small, and then throw out the large box.
1 2 3 4 5 6 7 8
int *data = newint[CAPACITY];
// fill data
int *temp = newint[LESS]; // get smaller box
// copy LESS elements from data to temp
delete [] data; // throw out the large box
data = temp; // pretend that you have always had the small box
// do other things with data
delete [] data; // remember to clean up
Do note that you will temporarily need CAPACITY+LESS space in order to get rid of CAPACITY-LESS elements.
there are 3 or 4 standard ways to 'delete' from an array.
1) you can use a sentinel. Say all your array values are positive, you could mark array[someindex] = -1 and that indicates it was deleted. Subsequent searches for positive values won't hit it, and printing you can check -1 and skip that one.
2) you can maintain a variable that tells you how many things are currently in the array. then you can do
2.a) swap the deleted one with the last one, and decrement the size variable (order is changed) or
2.b) copy array[someindex]= array[someindex+1] in a loop, moving all the data back a space (order kept)
3) you can make a resizing container of pointers to the array's elements and use that as a surrogate
4) you can make a new array and copy the old one into it, removing the offending element in the process, swap the pointers so the old one is the new one, and delete the old one (the delete keyword in this case is correct).
Better, you can use a c++ construct that directly supports this feature without all the work-arounds (AKA don't use arrays for problems not of fixed length).