[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Own assert() -> Segmentation fault
From: |
Ulrich Eckhardt |
Subject: |
Re: Own assert() -> Segmentation fault |
Date: |
Sun, 05 Nov 2006 19:12:35 +0100 |
User-agent: |
KNode/0.10.4 |
StreamKid wrote:
> why is a segmentation fault caused in this simple thing???
> and what exactly is this kind of error???
A segmentation fault occurs when you access memory you are not allowed to
access. Typically this is caused by an invalid pointer. If you want to
know where, use a debugger (typically gdb).
> *#define DEBUG
FYI: this macro has no meaning to standard C++, the one used to turn off
assert() is NDEBUG.
> *#define assert( x ) \
Bad: you are not allowed to define any names used by the standardlibrary.
> * if( ! ( x )) \
> * { \
> * printf( "\nERROR!! Assert %s failed", #x ); \
> * printf( "\n on line %s", __LINE__ ); \
> * }
Several things here:
- You could have used a single printf().
- The '\n' should come at the end of a line, not at the beginning.
- If you put this macro somewhere in the context of another if-else block,
it will mess up control flow. Similarly, it doesn't even require a
semicolon after it.
- If you compile this with warnings turned on, it will give you a warning
which is the cause of your segmentation fault.
Uli
--
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/