Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [C++] Explain assertion errors too me (https://thetfp.com/tfp/tilted-technology/55295-c-explain-assertion-errors-too-me.html)

WireX 05-11-2004 02:59 PM

[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];

would cause an assertion error. I realize that they wern't arrays, so I took it out and it worked fine, but what is an assertion error, what is an assertion, and why would it cause such an error to occur in some memory library?

PsychoMan 05-11-2004 05:15 PM

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.


All times are GMT -8. The time now is 01:39 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73