Help is to avoid arguing ... always!

In C and C ++, it assertis a very difficult procedure to write an error to stdoutand terminate the program. In our application, we implemented a much more reliable replacement for assert and gave it our own macro. We made every effort to replace it assertwith our macro, however there are many more ways assertyou can re-enter (for example, from internal third-party libraries, naive injection, etc.).

Any suggestions on how we can reduce, limit or even eradicate use assert? The best answer would be one that the compiler can catch for us, so we don’t need to babysit the code base as much as we currently do.

+5
source share
10 answers

I'm not sure I really understand the problem. Statements are only expensive if they go away, and this is normal, since now you are in an exception situation.

assertonly included in the debug assembly, so use a third-party library release assembly. But in fact, statements should not go away every minute.

+13
source

This will depend (at least in part) on what you are changing. Assuming that you do not mind printing your regular message and basically want to get rid of it by calling abort(), you can only leave assert()and define your own version instead abort().

, , abort() , , . ( Microsoft) , abort() , .

+2

( , , ). , , , (, assert() use SUPER_ASSERT() " - ), assert.h .

, , assert(), ( , assert.h).

+2

, . , :

  • .
  • . .

, , .

  • , ASSERT, , , , - #pragma once #ifndef __HEADERFILE_H__ #define __HEADERFILE_H__ . , ASSERT, .

  • assert.h cassert, , . , , . .

, , . , ASSERT, . . ASSERT , , , .

+2

, .

asm ( "int3" ), , . , , .

"ASSERT()" "assert()" assert().

+2

, , , assert(). , .. "_assert", , , .

assert(), , , ((void)0) . ((void)0) , - . ,

Assert(3 == x);

((void)0);

.

, , assert GUI. : , . . , , . , .

, , . , Assert(), ? , , . , -, , .

+1

assert() #define 'd ((void) 0) (#define NDEBUG), .

, ?

+1

:

#define NDEBUG
// Before
#include <assert.h>
// Or other header that includes assert.h

NDEBUG.

.

+1

, , , , "" assert. , . , , , . 99 100 . 99 100 , .. .

, , assert , - .

+1

assert ( , )

// #define assert(condition) ... /* old definition */
#define assert(condition) ((condition) & "PLEASE DO NOT USE ASSERT" = 42)
0
source

All Articles