comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
  1996-07-15  0:00 ` Stephen Leake
@ 1996-07-15  0:00 ` Fergus Henderson
  1996-07-15  0:00 ` Robert Dewar
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 13+ messages in thread
From: Fergus Henderson @ 1996-07-15  0:00 UTC (permalink / raw)



nasser@apldbio.com (Nasser Abbasi) writes:

>I am trying to keep an open mind about things, but see 
>for yourself, a simple program to test assertions, the
>Ada version works fine, the C++ core dumps after it
>displays the assertion statment. 

It's supposed to.  If you program fails an assertion, then
it has a bug; the core dump is there to help you debug it.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
@ 1996-07-15  0:00 ` Stephen Leake
  1996-07-15  0:00 ` Fergus Henderson
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 13+ messages in thread
From: Stephen Leake @ 1996-07-15  0:00 UTC (permalink / raw)



Nasser Abbasi wrote:
> 
> ... (simple example of assert, where Ada "works", and C++ "fails")
> C++ version
> ============
> .... now build and run
> 
> $CC t2.cc
> $a.out
> Assertion failed: i == 1, file t2.cc, line 8
> Abort (core dumped)
>       ^^^^^^^^^^^^^

Since this is the ANSI defined behaviour of a failed assertion, why do 
you claim it is not working? At least for ANSI C, assert is supposed to 
call Abort if the assertion fails. I assume the C++ behaviour is 
similarly defined.
> 
> --
> Nasser Abbasi. 

-- 
- Stephe




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
                   ` (2 preceding siblings ...)
  1996-07-15  0:00 ` Robert Dewar
@ 1996-07-15  0:00 ` Nasser Abbasi
  1996-07-16  0:00   ` Robert Dewar
                     ` (2 more replies)
  1996-07-15  0:00 ` Giuliano Carlini
                   ` (3 subsequent siblings)
  7 siblings, 3 replies; 13+ messages in thread
From: Nasser Abbasi @ 1996-07-15  0:00 UTC (permalink / raw)
  To: Nasser Abbasi



   From: nasser@apldbio.com (Nasser Abbasi)

ref (me): calling assert from C++ generates core dump, but
          not from Ada.


Someone just pointed out to me that C/C++ assert can generate
a core dump, and this is an expected behaviour.

So, It seems there is nothing wrong with the C++ program,
it core dumps becuase this is what it is supposed to do.

I think this is not natural behaviour, but since it is
documented like this, one can't argue with it.

Nasser




-- 
Nasser Abbasi. C/C++/Ada Solaris. GeneAssist - A client/server application 
for Nucleic acid and protein sequence search and analysis. 
Perkin Elmer - Applied BioSystem division. email:  nasser@apldbio.com   
MSEE(control), MSCS, MSCE, FM (Fide Chess Master).







^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
  1996-07-15  0:00 ` Stephen Leake
  1996-07-15  0:00 ` Fergus Henderson
@ 1996-07-15  0:00 ` Robert Dewar
  1996-07-15  0:00 ` Nasser Abbasi
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 13+ messages in thread
From: Robert Dewar @ 1996-07-15  0:00 UTC (permalink / raw)



Nasser complained:

$CC t2.cc
$a.out
Assertion failed: i == 1, file t2.cc, line 8
Abort (core dumped)


I don't find it so terrible to create a core dump here. In C terms since
there is no consistent way to signal such an event, calling abort and
dumping core so that they you have a debuggable image seems quite
reasonable. Find out if it is defined to do this, I would expect
that it is!





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Ada and C++ asserts.
@ 1996-07-15  0:00 Nasser Abbasi
  1996-07-15  0:00 ` Stephen Leake
                   ` (7 more replies)
  0 siblings, 8 replies; 13+ messages in thread
From: Nasser Abbasi @ 1996-07-15  0:00 UTC (permalink / raw)




I am trying to keep an open mind about things, but see 
for yourself, a simple program to test assertions, the
Ada version works fine, the C++ core dumps after it
displays the assertion statment. 

This is on Sun Solaris 2.5, using sun CC V4.0, and 
GNAT 3.05 . I might be doing something wrong in the 
C++ version, [if so, please tell me what I am 
doing wrong!] but I can not seem to see it. (after 
all, it is just a 2 line program ! ) May be this should
win an award for the smallest C++ program to core dump :)

ada version:
============

--------------------------------------------
with Ada.Text_Io; use Ada.Text_Io;
procedure Test_Assert is
 I: Integer := 0;
 begin

  pragma Assert(I = 1, "found i /= 1 !");
  null;

end Test_Assert;
----------------------------------------------

... now build and run it...

$gnatmake -gnata test_assert.adb

$test_assert

raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : found i /= 1 !


C++ version
============

-------------------------------------------
#include <assert.h>
main()
{
int i=0;

    assert(i == 1);
    return 0;
}
------------------------------------------

.... now build and run

$CC t2.cc
$a.out
Assertion failed: i == 1, file t2.cc, line 8
Abort (core dumped)
      ^^^^^^^^^^^^^


-- 
Nasser Abbasi. C/C++/Ada Solaris. GeneAssist - A client/server application 
for Nucleic acid and protein sequence search and analysis. 
Perkin Elmer - Applied BioSystem division. email:  nasser@apldbio.com   
MSEE(control), MSCS, MSCE, FM (Fide Chess Master).







^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
                   ` (3 preceding siblings ...)
  1996-07-15  0:00 ` Nasser Abbasi
@ 1996-07-15  0:00 ` Giuliano Carlini
  1996-07-16  0:00 ` Eric Kline
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 13+ messages in thread
From: Giuliano Carlini @ 1996-07-15  0:00 UTC (permalink / raw)



Nasser Abbasi wrote:
> 
> I am trying to keep an open mind about things, but see
> for yourself, a simple program to test assertions, the
> Ada version works fine, the C++ core dumps after it
> displays the assertion statment.

[SNIP: demonstration of Ada version working fine]

> C++ version
> ============
> 
> -------------------------------------------
> #include <assert.h>
> main()
> {
> int i=0;
> 
>     assert(i == 1);
>     return 0;
> }
> ------------------------------------------
> 
> .... now build and run
> 
> $CC t2.cc
> $a.out
> Assertion failed: i == 1, file t2.cc, line 8
> Abort (core dumped)
>       ^^^^^^^^^^^^^

I missed the articles leading up to this, and so I
may be misunderstanding what is being said. The only
reason this core dumps is because the implementation
of assert is something like this:
	#define assert(expr) \
		if ( !(expr) ) \
			assertFailed(#expr,__FILE__,__LINE__);
	void assertFailed( const char* expr, const char* file, unsigned line )
	{
		printf( "Assertion failed: %s, file %s, line %d\n", expr, file, line );
		abort();
	}
That is, the abort is intential. If you don't like it, it is easy to rewrite
assertFailed yourself. I've done this, and prefer:
	void assertFailed( const char* expr, const char* file, unsigned line )
	{
		throw AssertException( expr, file, line );
	}
No more core dump.

g




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 ` Nasser Abbasi
@ 1996-07-16  0:00   ` Robert Dewar
  1996-07-18  0:00   ` Ian Johnston
  1996-07-22  0:00   ` Richard Curnow
  2 siblings, 0 replies; 13+ messages in thread
From: Robert Dewar @ 1996-07-16  0:00 UTC (permalink / raw)



Nasser says

"I think this is not natural behaviour, but since it is
documented like this, one can't argue with it.

Nasser"


Actually I think that in the C environment it is entirely appropriate
behavior, because it means that you have a core image around that you
can debug. In fact it is quite reasonable that an Ada compiler should
call abort on an unhandled exception. GNAT does not, but it probably
should, or at least it should have an option to do so. After all you
can always put a when others at the outer level if you want to handle
such an exception -- an unhandled exception is really a program bug,
and calling abort is perfectly reasonable.

I really don't see why you would distinguish an assertion failure
from a segmentation fault. Basically the latter is a failure of
a hardware/OS assertion that addresses should be in range.





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
                   ` (5 preceding siblings ...)
  1996-07-16  0:00 ` Eric Kline
@ 1996-07-16  0:00 ` Ian Johnston
  1996-07-17  0:00 ` James McCallum
  7 siblings, 0 replies; 13+ messages in thread
From: Ian Johnston @ 1996-07-16  0:00 UTC (permalink / raw)



Nasser Abbasi wrote:

[Core dump after assertion failure]

It's meant to core dump after an assertion failure.

The failed assert ends up calling abort() -- which dumps
core.

Ian




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
                   ` (4 preceding siblings ...)
  1996-07-15  0:00 ` Giuliano Carlini
@ 1996-07-16  0:00 ` Eric Kline
  1996-07-16  0:00   ` Jon S Anthony
  1996-07-16  0:00 ` Ian Johnston
  1996-07-17  0:00 ` James McCallum
  7 siblings, 1 reply; 13+ messages in thread
From: Eric Kline @ 1996-07-16  0:00 UTC (permalink / raw)
  To: nasser


In C++, Asserts are to check the programmers mistakes not the
end users mistakes.  You dont use asserts to check users 
mistakes at all.  If you do, you will quickly learn that
asserts are only active in debug mode. So, You as the 
programmer will know what YOU did wrong







^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-16  0:00 ` Eric Kline
@ 1996-07-16  0:00   ` Jon S Anthony
  0 siblings, 0 replies; 13+ messages in thread
From: Jon S Anthony @ 1996-07-16  0:00 UTC (permalink / raw)



In article <4sf08d$1ij@peru.it.earthlink.net> Eric Kline <ekline@earthlink.net> writes:

> In C++, Asserts are to check the programmers mistakes not the
> end users mistakes.  You dont use asserts to check users 
> mistakes at all.  If you do, you will quickly learn that
> asserts are only active in debug mode. So, You as the 
> programmer will know what YOU did wrong

That's how assertions are used pretty much anywhere.  The user model
should be handled differently (at a higher level...)

/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
                   ` (6 preceding siblings ...)
  1996-07-16  0:00 ` Ian Johnston
@ 1996-07-17  0:00 ` James McCallum
  7 siblings, 0 replies; 13+ messages in thread
From: James McCallum @ 1996-07-17  0:00 UTC (permalink / raw)



In article <nh4tn9d9vw.fsf@paralysys>, nasser@apldbio.com (Nasser Abbasi) writes:
>
>I am trying to keep an open mind about things, but see 
>for yourself, a simple program to test assertions, the
>Ada version works fine, the C++ core dumps after it
>displays the assertion statment. 
>
>This is on Sun Solaris 2.5, using sun CC V4.0, and 
>GNAT 3.05 . I might be doing something wrong in the 
>C++ version, [if so, please tell me what I am 
>doing wrong!] but I can not seem to see it. (after 
>all, it is just a 2 line program ! ) May be this should
>win an award for the smallest C++ program to core dump :)
>
>ada version:
>============
>
[... ada version deleted ...]
>
>
>C++ version
>============
>
>-------------------------------------------
>#include <assert.h>
>main()
>{
>int i=0;
>
>    assert(i == 1);
>    return 0;
>}
>------------------------------------------
>
>..... now build and run
>
>$CC t2.cc
>$a.out
>Assertion failed: i == 1, file t2.cc, line 8
>Abort (core dumped)
>      ^^^^^^^^^^^^^
>
Form the Solaris man pages

assert(3C)             C Library Functions             assert(3C)
NAME
     assert - verify program assertion
...
DESCRIPTION
     This macro is useful for putting diagnostics into  programs.
     When it is executed, if expression is false (zero), assert()
     prints
          Assertion failed: expression, file xyz, line nnn
     on the standard error output and aborts....
                                      ^^^^^^

abort(3C)              C Library Functions              abort(3C)
NAME
     abort - terminate the process abnormally
...
NOTES
     Catching the signal is intended to provide  the  application
     writer  with a portable means to abort processing, free from
     possible  interference  from   any   implementation-provided
     library   functions.   If  SIGABRT  is  neither  caught  nor
     ignored, and the current directory is writable, a core  dump
     may be produced.
                                                       ^^^^


It looks as if the C++ version does work. :)

James. 

-- 
---
  James McCallum		 Acro Service Corp
  on contract to		 Engine and Processes Dept.
  Tel: +1 313 248 9017		 Ford Motor Co.
  e-mail:  external		 internal		profs
 	   jmccall4@ford.com 	 jmccall4@sl0270.srl	jmccall4






^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 ` Nasser Abbasi
  1996-07-16  0:00   ` Robert Dewar
@ 1996-07-18  0:00   ` Ian Johnston
  1996-07-22  0:00   ` Richard Curnow
  2 siblings, 0 replies; 13+ messages in thread
From: Ian Johnston @ 1996-07-18  0:00 UTC (permalink / raw)



Nasser Abbasi wrote:
> 
>    From: nasser@apldbio.com (Nasser Abbasi)
> 
> ref (me): calling assert from C++ generates core dump, but
>           not from Ada.
> 
> Someone just pointed out to me that C/C++ assert can generate
> a core dump, and this is an expected behaviour.
> 
> So, It seems there is nothing wrong with the C++ program,
> it core dumps becuase this is what it is supposed to do.
> 
> I think this is not natural behaviour, but since it is
> documented like this, one can't argue with it.

Given that C has no concept of exceptions, on many platforms
a core dump is a very reasonable thing to do. C++ has simply
'inherited' assert() from C.

On any UNIX, you can look at the core dump with a debugger
and get a stack trace showing not only where the core dump
happened, but the chain of function calls leading up to it,
along with parameter and variable values. Even on VMS,
you get a stack trace showing how you arrived at the assert.

What information does Ada supply about the circumstances leading
to the failed assertion?




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Ada and C++ asserts.
  1996-07-15  0:00 ` Nasser Abbasi
  1996-07-16  0:00   ` Robert Dewar
  1996-07-18  0:00   ` Ian Johnston
@ 1996-07-22  0:00   ` Richard Curnow
  2 siblings, 0 replies; 13+ messages in thread
From: Richard Curnow @ 1996-07-22  0:00 UTC (permalink / raw)




In article <31EE4C88.1AAD@ubs.com> Ian Johnston <ian.johnston@ubs.com> writes:

    Ian> On any UNIX, you can look at the core dump with a debugger
    Ian> and get a stack trace showing not only where the core dump
    Ian> happened, but the chain of function calls leading up to it,
    Ian> along with parameter and variable values. Even on VMS, you
    Ian> get a stack trace showing how you arrived at the assert.

    Ian> What information does Ada supply about the circumstances
    Ian> leading to the failed assertion?

With GNAT, the normal behaviour is to stop execution of the program
with a "raised SYSTEM.ASSERTIONS.ASSERT_FAILURE" message.  However,
during development, the program can be compiled with "-g" and then run
under a breakpoint debugger such as gdb.  If a breakpoint is set on
"system__assertions__raise_assert_failure" a stack trace can be
produced whenever an exception is raised due to an assertion being
failed.  A little unwieldy compared to a post-mortem gdb session on a
C or C++ -generated core file, but effective nevertheless.

Cheers
Richard
-- 
Richard P. Curnow
Stevenage, England




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~1996-07-22  0:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-15  0:00 Ada and C++ asserts Nasser Abbasi
1996-07-15  0:00 ` Stephen Leake
1996-07-15  0:00 ` Fergus Henderson
1996-07-15  0:00 ` Robert Dewar
1996-07-15  0:00 ` Nasser Abbasi
1996-07-16  0:00   ` Robert Dewar
1996-07-18  0:00   ` Ian Johnston
1996-07-22  0:00   ` Richard Curnow
1996-07-15  0:00 ` Giuliano Carlini
1996-07-16  0:00 ` Eric Kline
1996-07-16  0:00   ` Jon S Anthony
1996-07-16  0:00 ` Ian Johnston
1996-07-17  0:00 ` James McCallum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox