Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Assert

 

assert is a macro to do run-time debugging checks. After debugging, #define NDEBUG to let the preprocessor remove all asserts from your code.

 

Make use of assert [1-5]. The use of assert statements can help to document the assumptions you make when implementing your code [6].

 

assert is #defined in cassert.h.

 

 

 

 

 

Example

 

A division will only succeed if the denominator is unequal to zero. In your code, you will have to take care that a division by zero never occurs. Using assert, as shown in the code below, will take you to the problem directly.

 

#include <cassert>

int main()
{
  const double x = /* something */;
  const double y = /* something */;

  assert(y!=0.0); //Cannot divide by zero

  const double z = x / y;
}

 

 

 

 

 

Example user-defined assert

 

//---------------------------------------------------------------------------
/*
Assert, a custom assert macro
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppAssert.htm
//---------------------------------------------------------------------------
#ifndef BILDERBIKKEL_ASSERT_H
#define BILDERBIKKEL_ASSERT_H
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppAssert.htm
#ifdef NDEBUG_BILDERBIKKEL
  #define Assert(x) ((void)0)
#else
  #include <fstream>
  #include <iostream>
  #include <stdexcept>

  #define Assert(x)                      \
  if (!(x))                              \
  {                                      \
    std::cout                            \
      << "ERROR!! Assertion "            \
      <<  std::string (#x)               \
      <<  " failed on line "           \
      <<  (__LINE__)                     \
      <<  " in file "                  \
      <<  __FILE__                       \
      << std::endl;                      \
    std::ofstream f("assert_out.txt");   \
    f                                    \
      << "ERROR!! Assertion "            \
      <<  std::string (#x)               \
      <<  " failed on line "           \
      <<  (__LINE__)                     \
      <<  " in file "                  \
      <<  __FILE__                       \
      << std::endl;                      \
    f.close();                           \
      throw std::logic_error(            \
        "Assertion failed");             \
  }

#endif
//---------------------------------------------------------------------------
#endif // BILDERBIKKEL_ASSERT_H

 

 

 

 

 

User-defined asserts for specific IDE's

 

  1. Assert in a C++ Builder console Application
  2. Assert in a Qt Creator console Application

 

 

 

 

 

References

 

  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 68: 'Assert liberally to document internal assumptions and invariants'
  2. Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Advice 24.5.18: 'Explicitly express preconditions, postconditions, and other assertions as assertions'
  3. Steve McConnell. Code Complete (2nd edition). 2004. ISBN: -735619670. Chapter 8.2 'Assertions', paragraph 'Guidelines for using asserts': 'Use assertions to document and verify preconditions and postconditions'
  4. Steve McConnell. Code Complete (2nd edition). 2004. ISBN: -735619670. Chapter 8.2 'Assertions', paragraph 'Guidelines for using asserts': 'Use assertions for conditions that should never occur'.
  5. Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 24, chapter 'assert()': 'Use assert freely'
  6. John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Chapter 2.6: 'The use of assert statements can help to document the assumptions you make when implementing your code<'/li>

 

 

 

 

 

 

 

 

 

 

assert.h

 

//---------------------------------------------------------------------------
/*
Assert, a custom assert macro
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppAssert.htm
//---------------------------------------------------------------------------
#ifndef BILDERBIKKEL_QT_ASSERT_H
#define BILDERBIKKEL_QT_ASSERT_H
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppAssert.htm
#ifdef NDEBUG_BILDERBIKKEL
  #define Assert(x) ((void)0)
#else
  #include <fstream>
  #include <iostream>
  #include <stdexcept>

  #define Assert(x)                      \
  if (!(x))                              \
  {                                      \
    std::cout                            \
      << "ERROR!! Assertion "            \
      <<  std::string (#x)               \
      <<  " failed\n on line "           \
      <<  (__LINE__)                     \
      <<  "\n in file "                  \
      <<  __FILE__                       \
      << std::endl;                      \
    std::ofstream f("assert_out.txt");   \
    f                                    \
      << "ERROR!! Assertion "            \
      <<  std::string (#x)               \
      <<  " failed\n on line "           \
      <<  (__LINE__)                     \
      <<  "\n in file "                  \
      <<  __FILE__                       \
      << std::endl;                      \
    f.close();                           \
      throw std::logic_error(            \
        "Assertion failed");             \
  }

#endif
//---------------------------------------------------------------------------
#endif // BILDERBIKKEL_QT_ASSERT_H

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml