Ge To
Ge To

Reputation: 117

How can I deallocate the last element of an array in C++?

I have a dynamically allocated array like this: a[1,2,3,*unallocated value*] which has 4 elements. I also have a length variable from which I know how long it is.

I want to remove the last element in this array and set it to unallocated or uninitialize it somehow.

I think something like this might work a[lenght-1]=*something-something* but I don't know what to put on the end. Is there a way I can unallocate a single element? Or do I have to make a new array and copy all the elements into it except the last one?

Thanks in advance.

Upvotes: 0

Views: 1861

Answers (5)

Remy Lebeau
Remy Lebeau

Reputation: 596256

Since you have a length indicating the number of valid elements in the array, there is no need to copy values to a new array. Simply decrement the length. Depending on the data type used in the array, it may or may not make sense to reset the last element's value.

For instance, it doesn't really make sense to reset an integer at all, but you can if you want to, eg:

a[--lenght] = 0;

But, it does make sense to reset a std::string to free up any dynamic memory it may be using internally, eg:

a[--lenght] = "";
a[--lenght] = string();
--lenght;
a[lenght].clear();
a[length].shrink_to_fit(); 

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57698

To answer your question:

How can I deallocate the last element of an array in C++?

  1. Allocate a new array that is one less in capacity.
  2. Copy all items, except for last, to the newly created array.
  3. Delete the original array.

Example:

int *p_original_array = new int[5];
int *p_shorter_array  = new int[4];
//...
std::copy(p_original_array, p_original_array + 4, p_shorter_array);
delete [] p_original_array;

Upvotes: 1

dspr
dspr

Reputation: 2423

It's not clear if you want to remove elements by end only or if it can happen at any position.

In the first case, you could use the length variable to store the "used length" and another variable, maybe named "capacity", to store the actual capacity of your array. The first would never be greater than the second but it could be smaller. This is a very simple solution : if you put the size to 3, the index 3 is not usable anymore and you have nothing more to do.

In the second case, I would suggest the sentinel strategy that is described in the answer of @Vlad Feinstein.

Upvotes: 0

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

The *something-something* you are looking for is called sentinel.

For example, if your array could only contain positive integers, you could use the value 0 to indicate an "empty" slot. Or -1.

However, if ANY integer values are allowed, this won't work.

On the other hand - why do you need to place any value at all in that slot? You can simply rely on your length variable to figure if that element is valid or not.

Upvotes: 1

Simon Danninger
Simon Danninger

Reputation: 458

first you can always set something to a NULL pointer. But i think for your use case a vector is what you want to use. Vector uses array as data structure but you can add/remove elements at the end. (if vector fill's the array it will create a bigger array and copy everything so no need to worry about length)

Upvotes: 0

Related Questions