05-11-2004, 02:59 PM | #1 (permalink) |
Crazy
|
[C++] Explain assertion errors too me
Now, I know the cause of my error:
Psudo code of what I did: Code:
struct studentarray{ string fname; string lname; int grade; } studentarray *data[1000]; data[0] = new studentarray; delete [] data[0]; |
05-11-2004, 05:15 PM | #2 (permalink) |
Upright
Location: Waterloo, ON
|
Let's say you are writing a piece of code, and it is very important that the code behaves nicely for all input. An "assertion" is a way of making sure that your inputs are what you expect, and that your code is on the right track.
For example, let's say you are writing a divide function that takes two integers, "numerator" and "denominator", and returns the quotient. If denominator is zero, then what will your function return? The input doesn't really make sense in this context. In this case, you may be tempted to put an assertion at the start of your code. e.g. ASSERT(denominator != 0) When the program is running, it will throw an assertion error if someone passes a denominator of zero into your divide function. Typically assertions are only processed in debug mode so that you can identify and fix serious errors in your program before you compile an optimized release version. So, in this case, the standard C runtime memory manager has an assertion in its delete function. i.e. the array delete function asserts that the parameter is, in fact, an array. If you had run in release mode, it probably would have just crashed on that line, or corrupted memory. |
Tags |
assertion, errors, explain |
|
|