Why segmentation fault?
Why does this code cause a segmentation fault in linux?
I compile it with g++ filename.cpp -lcurses Code:
#include <stdio.h> It runs fine in Windows (except in windows i have to include conio.h instead of curses.h). |
Because conio getch and curses getch are different beasts.
|
Then I guess my question is, how do I get it to not give a segmentation fault in linux?
|
By properly using the (n)curses library, for example by initializing it with initscr and ending with endwin.
|
Ok I found out that putting initstr() before I use getch will solve the segmentation fault problem. But that command also clears the screen, which I don't want. Any ideas how to solve the segmentation fault without clearing the screen?
|
Sure, don't use curses but getchar and raw mode.
|
Ok, the statement system("stty raw -echo") will make getchar work like getch. That will do just fine. But how do I undo the system("stty raw -echo")?
|
Don't use system. Instead do
Code:
struct termios cur,raw; Code:
tcsetattr(STDIN_FILENO,TCSAFLUSH,&cur); |
Why shouldn't I use system?
|
Quote:
|
Quote:
|
Ok, thanks for the help everyone.
|
Hmm, I may be going about this the wrong way.
I need a non-blocking way to get a key, and getchar blocks. I need something lower-level than getchar, something that does not wait until the user presses a key, but rather gets whatever is in the keyboard buffer regardless if the user has pressed something or not. Basically, I want to have a loop, and in the loop I will check if a key has been pressed, and if it has then I will read that key, but if a key hasn't been pressed then I want to continue looping. It seems that there is a way to use getch in no-delay mode. Any way I can do this with getchar? |
setbuf(stdin,NULL) would work?
LE: I just remembered getchar uses stdin unbuffered. LE1: Usually getchar() waits for input from user, so I don't see what would be the problem. Guess I misunderstood you... |
I need a non-blocking getchar.
The non-blocking getch would work, but I would rather not use it because then I have to use the curses library and it does strange things to the terminal and behaves differently on windows/unix. |
All times are GMT -5. The time now is 12:20 AM. |