Deleting the end of an array

Started by
12 comments, last by Aardvajk 17 years, 6 months ago
hey everyone, im fairly ne to programming and am currently writing a graphics engine for pocket pc and am wondering if this

template<typename T>
void DeleteEndOfArray(T* Array, int Size, int NumToDelete)
{
    T* element = Array + Size - NumToDelete;
    for (int i = 0; i < NumToDelete; i++)
    {
        delete element++;
    }
}


will delete the end of the array and saying
delete[] Array;
later in my code will still work or if i have to use something like this

template<typename T>
T* DeleteEndOfArray(T* Array, int Size, int NumToDelete)
{
    T* tempArray = new T[Size - NumToDelete];
    memcpy(tempArray, Array, Size - NumToDelete);
    return Array;
}


or some one knows another alternative (i was going to use std::vector but read some were that it is to slow on embedded devices is this true?) thx in advance all EDIT: Typo left of element++ [Edited by - Julian90 on September 29, 2006 6:54:07 AM]
Advertisement
You'll end up in troubles with the code your presented. It isn't possible to delete any of the singular element of an array. Ie. if you allocated the array with new[] (and hopefully delete it with delete[]) you cannot delete a single element of the array with "delete". Beside, the code is deleting the same pointer quite many times which will hang your program.

You could write a resize function which makes a copy of the array and adds new elements to the end or drops out the elements which don't fit.

Otherwise, I'd go for std::vector. It keeps the data in continuous array and I assume that only overhead comes when resizing the array.

Cheers
Unfortunately, it is very likely that you'll get an exception !
When allocating an array, the compiler must stash its length somewhere.When you call operator delete[] , it retrieves the length to perform the necessary deallocation.

I would suggest using std::vector , or create your own linked-list template classes, but honestly - how slow can std::vector be ?

.

When you call new/new[], that memory allocated can only be freed by a corresponding call to delete/delete[] respectively.

No syntax exists for freeing part of an array.
Assuming we are talking about an array created with new[], no it won't work. It will crash horribly. You can only call delete e; if e is created by new as a single element like e=new element();

If you are determined to use raw arrays (and I'm not getting into the argument about std::vector because I don't know enough about embedded systems) then if you want to make the array smaller, you will have to allocate a new, smaller array, copy the elements in (correctly) and delete the original array.

You may be better off using a linked list. While this adds the overhead of a pointer-sized group of bytes to each element in your list, it means you can delete individual elements without having to move the rest of the elements in the list.

Also, bear in mind that using memcpy like that in your second example will only work correctly with PODs and will fail silently with unpredictable results if the array contains classes with dynamic data. std::copy would be a safer alternative and should reduce down to std::memcpy for PODs anyway.
Quote:Original post by Pro-XeX
....... how slow can std::vector be ?


itll depend on how his compiler implements it wont it?

Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Quote:Original post by grekster
Quote:Original post by Pro-XeX
....... how slow can std::vector be ?


itll depend on how his compiler implements it wont it?


To be a bit more precise, it depends on how well the library implements it versus how I, or you, implement it. I tend to assume a commercial standard library writer can do a better job than I can.
Quote:Original post by grekster
Quote:Original post by Pro-XeX
....... how slow can std::vector be ?


itll depend on how his compiler implements it wont it?



I don't think a compiler 'implements' anything, std::vector has constant time insertion and removal of elements at the end, I don't think this should differ
on embedded systems !

Nevertheless, if you use std::vector<T*> and store only pointers
to your objects, you can still use a function like
void DeleteEndOfArray(...)

If speed is such a tough issue, then you might look into using your custom
allocators for the std::vector class !

.

ok ill probally swap to std::vector but a linked list is out because i need to garuntee cach integrity, also i just tested this

int* array = new int[100];delete (array + 5);delete [] array;


and it runs fine but i think if array+5 were allocated for something else before the delete [] array thats when id see problems thx all
Quote:Original post by Pro-XeX
how slow can std::vector be ?


Personnaly, I'd say: "way faster than the OP kind-of-vector".

This topic is closed to new replies.

Advertisement