C++ assertions

A very simple feature of C++ called assertions can be used can in applications that want to safeguard itself from unwanted side-affects such as for e.g., reading/writing values beyond its boundary. Any condition that wants to be checked can be used in assert. The syntax of assert() is:
assert (some_conditional_expression);
for e.g., assert( 1 != 1) [will fail*]
assert( sizeof(int) != sizeof(int) ) [this one too]

The codes below are just a toys, BEWARE! heh.. ~:-D.

Assertions in C++ can be used by a simple statement called ‘assert (some criteria)’. Here’s a quick example.

#include “iostream”
#include “assert.h”

const int Z = 3;
//prime numbers
int prime[Z] = {2, 3, 5};

int main( )
int idx = 5;

assert(idx < Z); //will not go past this line because idx is greater than Z
//on my machine it nicely dumps core file*
//Before trying this example, you could disable coredumps if you are using unix-like OS
assert(idx = 0);

//2. comment out the topmost assert statement and see what happens below.
//this is a classic problem in testing where you check for reading/writing beyond the size of an array.
for (int i=0; i < idx; i++)
std::cout << &primes[i] < ” << primes[i] << ‘\n’; //if one follows #2, it’ll print garbage value past ‘5’ (the third value in prime[])
// this garbage could be any value that resides next to your prime[]arrays
// memory address and usually _it’s_undefinable_)

return 0;

One type of assertion-like mechanism used in pre-medievel C++ versions was something like this:

int boundary_23_error_func()
return 23; //23, then has to be documented somewhere for devs to be able to make some
//sense out of it and notify the upcalls about it in a meaningful way
//(e.g., a graphics program, a GUI or a webserver etcetera…)

int main( )


if (idx > Z)
return some_boundary_error_code; // or, return boundary_23_error_func();


I don’t think there’s anything wrong with using this kind of error handling code but when there are many thousand LOC, it quickly becomes a mess and using assert(some_bool_expression) anywhere you feel there should be a error condition check, is _much_^2 easier. Beware, though that it’s not a holy grail of error recovery. You don’t want system to fail just because it coredumped! Meaning, it should not be used in critical infrastructure that requires guaranteed uptime like maybe spaceships modules, aircraft etcetera.

* – on my machine it dumps core. core of a particular process in Unix-like OS contains that process’ memory image before it (abruptly) finishes execution. For more detail refer to core(5).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: