newbie Q: C's asserts in C++

K

KKramsch

Most of my programming is in C, so I'm a bit shaky on C++, but my
understanding is that "any C program is a C++ program". Assuming
that this is the case, suppose that a C++ program calls a function
from a library written in C, and that the C code for this function
has an "assert" statement. Is there some way for the calling C++
program to trap the exception that results when the assertion fails?

Thanks!

Karl
 
V

Victor Bazarov

KKramsch said:
Most of my programming is in C, so I'm a bit shaky on C++, but my
understanding is that "any C program is a C++ program".

That's not exactly true, it is more like "many C programs are also
C++ programs". There are well known exceptions.
Assuming
that this is the case, suppose that a C++ program calls a function
from a library written in C, and that the C code for this function
has an "assert" statement. Is there some way for the calling C++
program to trap the exception that results when the assertion fails?

"assert" is not a statement, it's a macro. It is supported in C++
just like in C with the same effect. You don't need to do anything
special.

If, however, you want to convert the behaviour of C's "assert" to
produce C++'s exception, I can't help you. If there is a way, I do
not know it.

Victor
 
P

Phlip

KKramsch said:
Most of my programming is in C, so I'm a bit shaky on C++, but my
understanding is that "any C program is a C++ program". Assuming
that this is the case, suppose that a C++ program calls a function
from a library written in C, and that the C code for this function
has an "assert" statement. Is there some way for the calling C++
program to trap the exception that results when the assertion fails?

The implementation of assert() is platform-specific, so you must ask this
question on a newsgroup that supports whatever compiler that C code used.

You could also carefully write your C++ calling code, possibly with lots of
its own assertions, to avoid that code failing.
 
T

The Square Root of Negative One

You could create your own assert function that throws exceptions.
Something like:

template<class T> void Assert(bool condition,
const char * message)
{
#ifndef NDEBUG
if(!condition)
throw T(message);
#endif
}

Standard C++ provides a good set of appropriate exception classes, such
as std::logic_error.
 
V

Victor Bazarov

Dfschweiss said:
Kind of stupid, since new is a reserved word in C++

I believe that's was the point Alex was making. Not every
valid C program is a valid C++ program...
 
R

Rade

KKramsch said:
Most of my programming is in C, so I'm a bit shaky on C++, but my
understanding is that "any C program is a C++ program". Assuming
that this is the case, suppose that a C++ program calls a function
from a library written in C, and that the C code for this function
has an "assert" statement. Is there some way for the calling C++
program to trap the exception that results when the assertion fails?

Thanks!

Karl

You shouldn't catch assertion failures.

Shortly, the idea behind the assert macro is that it checks conditions that
must be valid in the code. In other words, it catches something that CAN NOT
happen. For example, you have a non-NULL pointer to something, you check
that it is non-NULL, only you can call some function with this pointer and
it is vital for this function to receive non-NULL pointer. This function's
author can put assert in the function's code to enforce this condition. If
it is ever false, it must be a bug.

True, you can compile code with NDEBUG defined, and this usually wipes out
assert macro and other debugging stuff. So when you test, you compile
without NDEBUG and have the code that checks itself. When you ship your
code, you compile with NDEBUG and get slightly less secure version (but you
have tested it, haven't you ?), which lacks all these checks, so is much
more efficient.

Some library vendors also put asserts to check conditions that CAN NOT
happen with regular use of the library (but can happen if you don't use it
properly). Microsoft's ATL is an example. If this is the case, the library
is not inherently robust, and you have responsibility to learn the proper
use of the library.

From the other side, exceptions usually serve to detect real errors, i.e.
errors that CAN happen. However unlikely they are, but if they can happen,
assert should not be used to signalize them.

What about your case? You have assertion failures in the C library and you
want to get rid of them? Either this library is improperly used, and you
should then learn how to use it to avoid asserts. Or the library is buggy
itself and you should try to fix it or find a replacement. Good define
NDEBUG and recompile the library and you won't have assertion failures. Bad
this doesn't solve the real problem why this assert was placed there.

Cheers,
Rade
 
A

Alex Vinokur

Victor Bazarov said:
I believe that's was the point Alex was making.

Of course, it was my point.
Not every valid C program is a valid C++ program...

Also the program below.
---------
int main()
{
char a[3] = "ABC";
return 0;
}
 
A

Alex Vinokur

Alex Vinokur said:
Victor Bazarov said:
I believe that's was the point Alex was making.

Of course, it was my point.
Not every valid C program is a valid C++ program...

Also the program below.
---------
int main()
{
char a[3] = "ABC";
return 0;
}
---------

Also
---------
void foo() {}
int main()
{
foo(1);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top