PDA

View Full Version : Bus error while running program that uses curses.h




bahlquist
Oct 8, 2010, 01:58 PM
This program:

#include <curses.h>

main()
{
cbreak();
noecho();
printw("Hello, world!");
getch();
}

produces a bus error when run. This is curious, because I am not aware of any pointers being dereferenced here. I'm running this from unix on 10.5.



ulbador
Oct 8, 2010, 02:07 PM
produces a bus error when run. This is curious, because I am not aware of any pointers being dereferenced here. I'm running this from unix on 10.5.

You have to call initscr() before you call any curses functions. Otherwise curses doesn't know the terminal type and anything about stdout/err/etc

bahlquist
Oct 8, 2010, 02:14 PM
Thanks!

ulbador
Oct 8, 2010, 02:16 PM
Make sure you are a good little programmer and call endwin() at the end of your program too. Weird and odd things can happen to your terminal if your program exits and curses hasn't fully disconnected itself (though you probably wouldn't see these artifacts on this small of a program)

That's why if you've ever used many curses or ncurses programs and you hit CTRL-C, the terminal stays weird colors or acts funny. The programmer didn't trap that signal and call endwin() before the program terminated.

And with what you have there, the printw might not visibly print anything until you call the refresh() function. Printw prints to a hidden window which is not updated to the main screen until you call refresh().