LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2006, 11:12 AM   #1
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Rep: Reputation: 15
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).
 
Old 12-07-2006, 11:28 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Because conio getch and curses getch are different beasts.
 
Old 12-07-2006, 11:29 AM   #3
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Original Poster
Rep: Reputation: 15
Then I guess my question is, how do I get it to not give a segmentation fault in linux?
 
Old 12-07-2006, 11:34 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
By properly using the (n)curses library, for example by initializing it with initscr and ending with endwin.
 
Old 12-07-2006, 11:34 AM   #5
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Original Poster
Rep: Reputation: 15
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?
 
Old 12-07-2006, 11:37 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Sure, don't use curses but getchar and raw mode.
 
Old 12-07-2006, 11:46 AM   #7
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Original Poster
Rep: Reputation: 15
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")?
 
Old 12-07-2006, 12:01 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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);
 
Old 12-07-2006, 12:03 PM   #9
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Original Poster
Rep: Reputation: 15
Why shouldn't I use system?
 
Old 12-07-2006, 12:18 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by Superdude
Why shouldn't I use system?
Why spawn an extra process when you don't need to?
 
Old 12-07-2006, 12:42 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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").
 
Old 12-07-2006, 01:03 PM   #12
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Original Poster
Rep: Reputation: 15
Ok, thanks for the help everyone.
 
Old 12-07-2006, 01:36 PM   #13
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Original Poster
Rep: Reputation: 15
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?

Last edited by Superdude; 12-07-2006 at 01:39 PM.
 
Old 12-07-2006, 01:41 PM   #14
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
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...

Last edited by BiThian; 12-07-2006 at 01:52 PM.
 
Old 12-07-2006, 01:51 PM   #15
Superdude
Member
 
Registered: Oct 2003
Distribution: Slackware 9.0
Posts: 47

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault samprabhu Linux - General 3 07-29-2005 07:17 PM
segmentation fault ! freelinuxcpp Linux - Software 7 12-15-2003 06:34 AM
segmentation fault... perdesiz Linux - Software 1 08-18-2003 01:55 AM
Segmentation Fault live2 Linux - General 6 03-02-2003 08:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration