problems with getch()

problems with getch()

Post by Harald Aistinge » Thu, 27 Aug 1998 04:00:00



Hello there !

Hope somebody could help me.
When I write a program  which contains the getch() command, I get a
"segmemtation fault" when I try to run the executable.
For example: after compiling this very simple program(I use such an easy
one because it shows my problem the best way) I try to run it and get a
segmentation fault.

#include <stdio.h>
#include <curses.h>                               /*  Please tell me if
this isn't correct */

main()
{
            char c;

            printf("Enter a letter: ");
            c = getch();                                /* If I used
getchar() it'd run without any problems */
            printf("%c", c); }

Now I compile it like this:
                                            gcc program.c -lncurses -o
program
The compiler does its work but when I type the name of the program I get
the seg. fault.
How could I manage that it runs correctly ?
Repost or just write me an e-mail.

Thanks and kind regards,

Harald Aistinger
PS: I use the GNU-C-compiler.

 
 
 

problems with getch()

Post by cra.. » Thu, 27 Aug 1998 04:00:00



Quote:> When I write a program  which contains the getch() command, I get a
> "segmemtation fault" when I try to run the executable.

From the curses man page:

       initscr  is normally the first curses routine to call when
       initializing a program.  A few special routines  sometimes
       need  to  be called before it; these are slk_init, filter,
       ripoffline, use_env.  For multiple-terminal  applications,
       newterm may be called before initscr.
...
       A  program  should  always  call  endwin before exiting or
       escaping  from  curses  mode  temporarily.   This  routine
       restores  tty  modes,  moves the cursor to the lower left-
       hand corner of the screen and resets the terminal into the
       proper non-visual mode.  Calling refresh or doupdate after

#include <curses.h>
int main()
{
        int c;

        initscr();
        cbreak();
        c = getch();
        endwin();
        return ( c);

Quote:}


 
 
 

problems with getch()

Post by Klaus Schillin » Thu, 27 Aug 1998 04:00:00



> Hello there !

> Hope somebody could help me.
> When I write a program  which contains the getch() command, I get a
> "segmemtation fault" when I try to run the executable.
> For example: after compiling this very simple program(I use such an easy
> one because it shows my problem the best way) I try to run it and get a
> segmentation fault.

> #include <stdio.h>

stdio.h is called by curses.h, so it's obsolete including it (but not wrong)

Quote:> #include <curses.h>                               /*  Please tell me if
> this isn't correct */

> main()
> {
>             char c;

>             printf("Enter a letter: ");
>             c = getch();                                /* If I used
> getchar() it'd run without any problems */
>             printf("%c", c); }

You need to have the terminal initialized before calling getch ().
This can be done by calling initscr () before getch. If you don't do that,
the window pointer variables stdscr and curscr don't get initialized correctly.
stdscr is used by getch, and when it's not initialized correctly, it points
into garbage and thus triggers a segfault.
In addition, you need to set the terminal into cbreak mode for getch to work
correctly, by calling cbreak (), somewhen after initscr() and before getch ().
But initscr () is most important for curses programs. (With a few exceptions
that experts like T.Dickey might point out occasionally.)

Quote:> Now I compile it like this:
>                                             gcc program.c -lncurses -o
> program
> The compiler does its work but when I type the name of the program I get
> the seg. fault.

Segfaults are best analyzed by using the gdb.

Klaus Schilling