newbie Q: C's asserts in C++

newbie Q: C's asserts in C++

Post by KKramsc » Sat, 04 Dec 2004 11:26:40



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

--
Sent from a spam-bucket account; I check it once in a blue moon. If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.
 
 
 

newbie Q: C's asserts in C++

Post by Victor Baz » Sat, 04 Dec 2004 12:00:08

"KKramsch" < XXXX@XXXXX.COM > wrote...

That's not exactly true, it is more like "many C programs are also
C++ programs". There are well known exceptions.


"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

 
 
 

newbie Q: C's asserts in C++

Post by Phli » Sat, 04 Dec 2004 12:56:21


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.

--
Phlip
http://www.yqcomputer.com/
 
 
 

newbie Q: C's asserts in C++

Post by Alex Vinok » Sat, 04 Dec 2004 16:23:51


[snip]

Try to compile the following program with C and C++ compilers.

----------
int main()
{
int new;
return 0;
}
----------

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://www.yqcomputer.com/
http://www.yqcomputer.com/
 
 
 

newbie Q: C's asserts in C++

Post by The Square » Sat, 04 Dec 2004 18:16:49

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.

In article <YEQrd.127132$V41.50317@attbi_s52>,




----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.yqcomputer.com/ The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
 
 
 

newbie Q: C's asserts in C++

Post by dfschweis » Sun, 05 Dec 2004 00:45:41

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

newbie Q: C's asserts in C++

Post by Victor Baz » Sun, 05 Dec 2004 00:55:01


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

newbie Q: C's asserts in C++

Post by Default Us » Sun, 05 Dec 2004 03:02:02


So I guess it'd be some sort of counter-example then, wouldn't it?



Brian
 
 
 

newbie Q: C's asserts in C++

Post by Rade » Sun, 05 Dec 2004 07:12:55


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

NDEBUG and recompile the library and you won't have assertion failures. Bad


Cheers,
Rade
 
 
 

newbie Q: C's asserts in C++

Post by Alex Vinok » Sun, 05 Dec 2004 21:18:14


Of course, it was my point.


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

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://www.yqcomputer.com/
http://www.yqcomputer.com/
 
 
 

newbie Q: C's asserts in C++

Post by Alex Vinok » Wed, 08 Dec 2004 17:27:56


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

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://www.yqcomputer.com/
http://www.yqcomputer.com/