Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ assert implementation in assert.h

00001 /* assert.h
00002    Copyright (C) 2001, 2003 Free Software Foundation, Inc.
00003    Written by Stephane Carrez ([email protected])       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00028 #ifndef _ASSERT_H
00029 #define _ASSERT_H
00030 
00031 #ifdef NDEBUG
00032 # define assert(EX)
00033 #else
00034 # define assert(EX) (void)((EX) || (__assert (#EX, __FILE__, __LINE__),0))
00035 #endif
00036 
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 
00041 extern void __assert (const char *msg, const char *file, int line);
00042 
00043 #ifdef __cplusplus
00044 };
00045 #endif
00046 #endif

The question is: what is for the "(void)" on line 34 and what is __assert?

like image 776
Narek Avatar asked Mar 14 '12 11:03

Narek


People also ask

What does assert H do in C?

The assert. h header file of the C Standard Library provides a macro called assert which can be used to verify assumptions made by the program and print a diagnostic message if this assumption is false.

Is assert H standard C?

h is a header file in the standard library of the C programming language that defines the C preprocessor macro assert() . In C++ it is also available through the <cassert> header file.

Why do we use assert H in C++?

Answer: An assert in C++ is a predefined macro using which we can test certain assumptions that are set in the program. When the conditional expression in an assert statement is set to true, the program continues normally. But when the expression is false, an error message is issued and the program is terminated.

What is the difference between assert () and Static_assert ()? Select one?

static_assert is meant to make compilation fail with the specified message, while traditional assert is meant to end the execution of your program.


1 Answers

Look at this line:

extern void __assert (const char *msg, const char *file, int line);

__assert is function that takes an assertion message, a file name and a line number as arguments. Basically, this is the method that prints out the error message and terminates the program when the assertion failed.

Then look at the macro definition above:

#define assert(EX) (void)((EX) || (__assert (#EX, __FILE__, __LINE__),0))

It defines the assert(EX) macro so, it first checks the EX expression and (because of the short-circuit operation of the C++ || operator) only if it fails, it calls the __assert function and passes the failed assertion exception as a string, and the exact location of the assert() method call in your source files. With this preprocessor trickery the assertion library achieves that when you type the following in your program

assert(a == 0);

and your assertion fails during the program run, you get the detailed

Assertion failed: a == 0 at program.c, line 23

error message that helps you to identify the exact location where the assertion was failing in your code.

The (void) part is just for make sure that the compiler won't put up some warnings about the unused result of the (EX) || 0 expression, see the other answers, the guys explained it well.

The remaining preprocessor define NDEBUG is used to turn of assertion generation at all compile-time, you your resulting executable will be smaller and faster.

like image 166
buc Avatar answered Oct 09 '22 16:10

buc