There is a logical issue with your use of assert
: one should not use it for checking conditions that may be false, assuming that the program is written correctly.
The reason the asserts exist in the first place is to let you make statements in your program about your assumptions about the state of the program. Triggering an assert should indicate an error in your program's logic, and nothing else. In particular, it should not be used to detect a situation when you run out of memory, which is something that could reasonably happen even when your program is 100% correct.
When you write, for example,
int i = getIndex();
assert(i > 0);
you tell the compiler and the readers of your code that your algorithm is such that getIndex()
could not possibly return a negative number. By using an assert you say that if it does, then there is an error in your code. Using asserts to check runtime conditions would be a misuse of that feature.