LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why segmentation fault? (https://www.linuxquestions.org/questions/programming-9/why-segmentation-fault-508240/)

Superdude 12-07-2006 11:12 AM

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>
#include <curses.h>

int main() {
    int i;
    i = getch();
    printf("%d", i);
}


It runs fine in Windows (except in windows i have to include conio.h instead of curses.h).

jlliagre 12-07-2006 11:28 AM

Because conio getch and curses getch are different beasts.

Superdude 12-07-2006 11:29 AM

Then I guess my question is, how do I get it to not give a segmentation fault in linux?

jlliagre 12-07-2006 11:34 AM

By properly using the (n)curses library, for example by initializing it with initscr and ending with endwin.

Superdude 12-07-2006 11:34 AM

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?

jlliagre 12-07-2006 11:37 AM

Sure, don't use curses but getchar and raw mode.

Superdude 12-07-2006 11:46 AM

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")?

tuxdev 12-07-2006 12:01 PM

Don't use system. Instead do
Code:

  struct termios cur,raw;
  tcgetattr(STDIN_FILENO,&cur);
  raw=cur;
  raw.c_lflag&=~(ICANON|ECHO|ISIG);
  raw.c_cc[VMIN]=1;
  raw.c_cc[VTIME]=0;
  tcsetattr(STDIN_FILENO,TCSAFLUSH,&raw);

then later, to set it back,
Code:

  tcsetattr(STDIN_FILENO,TCSAFLUSH,&cur);

Superdude 12-07-2006 12:03 PM

Why shouldn't I use system?

matthewg42 12-07-2006 12:18 PM

Quote:

Originally Posted by Superdude
Why shouldn't I use system?

Why spawn an extra process when you don't need to?

jlliagre 12-07-2006 12:42 PM

Quote:

Originally Posted by Superdude
But how do I undo the system("stty raw -echo")?

tuxdev and matthewg42 are right, but if you insist, you still can undo with system("stty -raw echo").

Superdude 12-07-2006 01:03 PM

Ok, thanks for the help everyone.

Superdude 12-07-2006 01:36 PM

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?

BiThian 12-07-2006 01:41 PM

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...

Superdude 12-07-2006 01:51 PM

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.