delete[] question

If I declare an array in a class with

1
2
  float* arrlist;
arrlist = new float [SZ];


and run through a loop assigning values in the array, do I have to delete every element in array one at a time or can I just use

 
delete[] arrlist;


This is a pointer to an array of type float right, not an array to pointer floats, so I don't actually have to loop through to delete each element/pointer correct??
Last edited on
Can you give the line(s) of code that allocate the memory?
1
2
3
int * foo = new int[10];
delete[] foo; // will kill the entire array (each individual element of the array that is).
delete foo; // will only kill the first bit of the array, but the rest of the array will still be allocated. 
Spikerocks101 wrote:
1
2
3
int * foo = new int[10];
delete[] foo; // will kill the entire array (each individual element of the array that is).
delete foo; // will only kill the first bit of the array, but the rest of the array will still be allocated. 


The delete on line 3 results in undefined behavior. Line 2 is the correct way to do this.

@OP: If you didn't new it, don't delete it.
Topic archived. No new replies allowed.