> But at the end of the run it comes up with an Debug Assertion failed.
> WHY???
In your case, you're deleting what you didn't new.
If you don't call new for a pointer, don't call delete.
Examples.
int *p = new int ; .... delete p;
int *q = new int[10]; ... delete [] q;
If you used [] in the new, then you MUST use [] in the delete as well.
Other causes of trouble
- running off the end of the allocated memory (say by using <= in a loop rather than < ) MAY cause an assertion, or a segfault
- trying to delete the same pointer twice MAY cause an assertion, or a segfault
- doing some pointer calculations, and then deleting a pointer which is no longer at the start of the memory allocated MAY cause an assertion, or a segfault
Note that just because you do something wrong, there is NO guarantee of a run-time diagnostic. The surprise can be deferred until much later on.