You will be creating a new C++ file in your terminal. This must be done using the query “touch” within the shell. Thus, the query used for this purpose has been displayed in the image beneath.
Now, the file is placed in the home file explorer of the Linux system. We have to open this c++ file within one of our installed editors, i.e., vim, text, or nano. So, we have been choosing the GNU Nano editor for this purpose. The command to open this new file is shown below.
Let’s begin with the very first example of using the assert statement in the C++ code to validate the logic of some code statements. We have started the code with the header files. The addition of an input-output stream header is necessary when you are working within the C++ platform. The standard namespace is a must in C++ with the keyword “using” and “Std” in it, as you can see from the image as well. For the “assert” statement user, we need to add the header file of assert to make it functional. Thus, we have added the “Assert.h” header file with the “#include” keyword. The main() method has been started to do the execution.
We have initialized an integer variable named “val” with a value of 13. At the next consecutive line, the variable “val” has been overridden with another value of 15. The assert statement has been used after that to check whether the variable “val” has equal to the value 13. As it is logically incorrect and the variable “val” has been overridden with value 15, it will throw an exception. Let’s save the code after the completion of a main() function with “Ctrl+S”. After saving, we need to get back to the terminal for compilation and execution purposes with “Ctrl+X”.
For the C++ code compilation, you must need some compiler configured in your Ubuntu 20.04 system. Thus, we have been using the g++ compilation instruction in the shell to compile the file “assert.cc”. The compilation returns nothing in return is making us clear that code is syntactically correct throughout. Let’s just execute the file with our everlasting “./a.out” command in the console. The running of a file returns the error in return as the assert statement returns false in return. The output is presented in the image affixed.
If you want to avoid the asserted error output by the assert statement, then you have to define the No Debug flag within your C++ code. For that purpose, you have to use the keyword “define” with the hash sign and “NDEBUG” as a flag in the header area. Thus, we opened the same file and added the namespace after the “io” header file. After the standard namespace, we have defined the “NDEBUG” flag in the header and used the “assert.h” header after it. Make sure to not change the sequence of headers shown below. Within the main() function, we have added the cout statement to output that the assert disabling has been successful.
Let’s compile and run this updated code again in the shell. Upon doing that, we have got no errors this time by using the “NDEBUG” in our header area.
Let’s have a new example of using the assert statement in the C++ code. But this time, we will be using the assert statement within some user-defined function. Thus, we have added the header files and standard namespace at the start of the code in a sequence. The user-define method named “show()” has been declared, taking integer type pointer value in its parameter.
Within the show() function, we have been using the “assert” statement to see if the pointer passed in the parameter is not NULL. If it’s null, the error will be thrown. The cout statement will then display the value of a variable the pointer “v” is pointing to. Within the main function, we have initialized an integer variable “val” with a value of 13. Two pointers “a” and “b” have been initialized to NULL. The address of variable “val” has been bound with pointer “a”. The show() function has been called with passing pointer “a” and “b” separately to output different results. The second call to show function must throw an exception because it takes the NULL pointer, and the assert statement returns false in return. Let’s save and run the code.
After running this piece of code, we have got the same result as we have discussed above. It shows result 13 for the first function call, i.e., passed pointer “a”, while the other function call shows the error.
To rectify this error, you just need to bind the pointer “b” with the variable “val” as well. Thus, we have done the same within the below-shown code. We have replaced the variable “val” with v1 and added another variable, “v2”. The variable v1 has been bound with pointer “a” and v2 has been bound with pointer “b”. The overall script remains the same as shown.
After the code debugging and running, no errors have been found so far. Both the values for v1 and v2 variables have been displayed successfully, as shown below.
Let’s end with the last but not the least example of an assert statement in C++ code. The code contains the two assert statements and two cout statements. The first assert statement is checking if the multiplication results in the same output. As the condition is correct logically, it must return true, and no error will be shown. The cout statement will be executed displaying that the first statement was correct. The other assert statement is again checking if the multiplication at both sides returns the same output. As the output would not be the same, it will be logically incorrect. Hence, it will be returning false. Due to this, the error will occur, and the cout statement will not be executed after that.
Due to logical error, the program ends automatically after the execution of the first cout.
To resolve this issue, we will be using the “NDEBUG” flag in the code as we have done it so far.
This time, no error has been displayed, and both the cout statements are executed.
The article is all about C++ assert statement usage. We have tried our best to demonstrate its working through the main() function and user-defined function. We have also discussed the way to disable the assert in C++, i.e., the NDEBUG flag. We are hoping that all the examples will provide you with immense help.