Heap Corruption with triple pointers

Hello - I am having trouble deleting a triple pointer. Below is my code.

complex <double> ***arr;
[Fill in arr]
for (int n1=0;n1<N1;n1++) {
for (int n2=0;n2<N2;n2++) {
delete [] arr[n1][n2];
}
delete [] arr[n1];
}
delete [] arr;

The strange part is that when N1=1 the program works fine but when N1>1 the line delete [] arr gives a heap error.
Heap corruption isn't caused by delete[].

The problem is you are stepping outside of array bounds somewhere and writing to space you shouldn't be. That is corrupting the heap. (EDIT: so, to clarify, the problem is probably in the [Fill in arr] code you didnt' post /EDIT)

The reason the program is stopping at the delete[] line is because delete[] actually examines the heap and notices that it's been messed up. But that doesn't really tell you where in your code it's actually being messed up.

One approach you can take to try and find the problem:

- find all the code that accesses 'arr'
- start commenting out blocks of it and run until the problem goes away
- once you narrow down which area of code the problem is occuring in, examine it closely to make sure you're not stepping out of bounds. If necessary, step through it in a debugger.


Alternatively... you could put this 'arr' thing in a class and access the data with get functions (or an operator overload). This way you can build in bounds checking.

Or... you could use a std container class (like a vector) and use at() to do bounds checking.
Last edited on
Topic archived. No new replies allowed.