Delite New

When I make an array of new objects and write delite arraySelector, will that delite all of the array elements or just the first one?
- I think you are referring to when you allocate memory for an array.

- When you allocate memory for an array, space is basically reserved for that array (or even variable or object in other cases) in memory.

- When you delete the array, or variable, that you that you allocated space for, you are freeing that memory to be used for something else.
Good, so I free alocated space by 'delete array'.
But if I wanted to get rid of (and free space) of just one, lets say last, element of the array, would I delite it by 'delete array[3]' ?
Last edited on
And what if I had an array of pointers to alocated memory data, could I free it all at once wit 'delete arr'?
If you use p = new T[X] where p is of type T*, T is a type, and X is a number, then you free the array with delete[] p. This frees the entire array. There is no method to free "just one element".

If you use p = new T, then you free the single object with delete p.
Each new returns a contiguous piece of memory. Each piece of memory returned by a call to new should be deleted by exactly one delete.

So if your arrays pointer elements have been allocated by seperate calls to new, you also have to delete them seperatly.
if array of alocated memory locations is delited by delete[] p;
what will happen if I insted write delete p; like for single elements?
Last edited on


zoran404 wrote:
if array of alocated memory locations is delited by delete[] p;
what will happen if I insted write delete p; like for single elements?


- I'm not sure honestly however, it will most likely not be anything positive.

- I just suggest that you don't do that.
in
delete[] p;
do I have to specify the lenght like
delete[4] p; ?
if array of alocated memory locations is delited by delete[] p;
what will happen if I insted write delete p; like for single elements?


This doesn't quite make sense to me. If something allocated thusly: p = new P[] and is then deleted like so: delete p instead of delete [] p your code invokes undefined behavior, and anything can happen, including your program working normally or a pink elephant materializing above your head and crushing you to death when it falls.


do I have to specify the lenght like
delete[4] p; ?

No.
Topic archived. No new replies allowed.