[prog] use of assert (was: ncurses forms library)

Elizabeth Barham lizzy at soggytrousers.net
Fri Oct 31 23:18:29 EST 2003


[This message sent to the programming mailing list of linuxchix
because of its informative nature.]

ed writes:

> Thank you for your informative reply to my question on how to obtain
> the user's input when using the ncurses forms library.  I have
> searched all the documents I have on Ncurses, which are few enough,
> such as the HOWTO, Writting Programs in Ncurses by Raymond, and my
> man documentation, but I can not find any reference to the assert
> command as you have described:
>    assert(buff != 0);
> My only question is, where does one find this information? Is there
> a book on Ncurses?
> Or a course of some kind? 
>    Any information would be most appreciated!

Hi Ed,

   Oops. Assert is separate from ncurses and is in assert.h:

#include <assert.h>
#include <stdio.h>

int main(int argc, char * argv[]) {
  assert(0);
}

for example results in:

test: test.c:6: main: Assertion `0' failed.
Aborted

It is used to assert that a condition is true or else the program
errors with an informative message. It is often used when debugging:

ASSERT(3)           Linux Programmer's Manual           ASSERT(3)

NAME
       assert - Abort the program if assertion is false.

SYNOPSIS
       #include <assert.h>

       void assert (int expression);

DESCRIPTION
       assert()  prints  an  error message to standard output and
       terminates the program by calling abort() if expression is
       false  (i.e.,  compares equal to zero).  This only happens
       when the macro NDEBUG was undefined  when  <assert.h>  was
       last included.

RETURN VALUE
       No value is returned.

CONFORMING TO
       ISO9899  (ANSI  C).   In  the 1990 standard, expression is
       required to be of type int and undefined behavior  results
       if  it  is  not,  but in the 1999 standard it may have any
       scalar type.

BUGS
       assert() is implemented as  a  macro;  if  the  expression
       tested  has side - effects, program behaviour will be dif­
       ferent depending on whether NDEBUG is  defined.  This  may
       create  Heisenbugs  which go away when debugging is turned
       on.

SEE ALSO
       exit(3), abort(3)

GNU                         1993-04-04                  ASSERT(3)

So, if you compile the above test program (test.c) as:

  gcc -o test-with-assert test.c

you'll receive the above error message. OTOH if you compile it as:

  gcc -o test-without-assert -DNDEBUG test.c

you won't receive an error message of any kind. In the example of
using ncurses forms, I was using it to insure that the return value
was not null and assumed you were familiar with assert's usage. My
apologies.

   As to a book on ncurses, there is a book on curses by O'Reilly:

          http://www.oreilly.com/catalog/curses/

and in fact I had a copy at one time but I probably gave it to the
library or sold it at half-price books some time back. ncurses is an
extension of curses and as far as I know ncurses implements all of
curses.

   Half.com has one for $3.75:

         http://half.ebay.com/cat/buy/prod.cgi?cpid=1534135&meta_id=1

Regards,
Elizabeth


More information about the Programming mailing list