curses.h, beep() returns ERR, flash() casuses segment fault.

Polytropon freebsd at edvax.de
Thu Aug 28 04:42:34 UTC 2008


Good morning!

On Wed, 27 Aug 2008 19:10:40 -0700 (PDT), Christopher Joyner <chris27wjoyner at yahoo.com> wrote:
> I do not get the OK from beep, and flash crashes the program.
> This is my code:
> 
> #include <curses.h>
> 
> int main(int argc,char** argv)
> {
>     if(beep()!=OK)
>         printf("No OK\n"); fflush(stdout);
>     if(flash()!=OK)
>         printf("No Flash\n"); fflush(stdout);
> return 0;
> }

First of all, fflush seems to need a definition from stdio.h.
I'm not sure if it's included by default (such as -lc is).

I tried to link with -lcurses and -lncurses and I can reproduce
the error you mentioned:



/* beepflash.c */

#include <stdio.h>
#include <curses.h>

int main(int argc, char **argv)
{
	printf("beep: %d\n", beep());
	fflush(stdout);

	printf("flash: %d\n", flash());
	fflush(stdout);

	return 0;
}



% cc -Wall -lncurses -o beepflash beepflash.c
% ./beepflash
-1
Segmentation fault (core dumped)

Allthough I did a lot of programming with NCurses, I'm not sure
where this error comes from. My old programs do work with the
ncurses installation I have (compile + run), but none of them
uses flash() or beep(). I found out that no ncurses package is
installed.

So I did

	# pkg_add -r ncurses

to install devel/ncurses (ncurses-5.6_2) and repeated the compile
process - same result. So I went to check the main difference
between my working programs and your code, because I noticed that
if I put a beep() and a flash() call into my program, they worked
as expected.

Solution: Prior to any call to the ncurses library, put these
into your code:

	initscr();
	cbreak();
	noecho();
	nonl();
	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);
	start_color();

I'm not sure which are essential to make beep() and flash() work
as expected, but you can easily find out which ones you don't need.

Explaination: The ncurses library / seesion doesn't seem to be
initialized correctly, that's why beep() didn't work and flash()
caused a segmentation fault.



-- 
Polytropon
>From Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list