Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Segmentation fault (core dumped) Problem

  1. #1
    Join Date
    Jun 2012
    Beans
    22

    Segmentation fault (core dumped) Problem

    Ok, I am sure this has already been solved somewhere, but I haven't found it yet. Anyway, I am writing a program for class and I keep getting this 'error' when I run it. I am using kate/gedit as my text editor, coding in C++, and compiling with g++. when I run my program (which is very small, [hangman]) i get: 'Segmentation fault (core dumped)'. So,

    1. What does that mean?
    2. Where does it come from/ where is the core being 'dumped'?
    3. How do I fix that problem? or can I even fix it?

    Thank you in advance. I'm kind of a novice Linux user as I am still learning a lot of things.

  2. #2
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Segmentation fault (core dumped) Problem

    Thread moved to Programming Talk.

    Welcome to the forum.

    Sometimes a segmentation fault can be a symptom of a rogue pointer.
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.

  3. #3
    Join Date
    Mar 2008
    Location
    Queensland Australia
    Beans
    664
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Segmentation fault (core dumped) Problem

    Install CodeBlocks from the package manager and run your program in debugging mode. Then when you get your "segmentation fault" you will be able to use the backtrace to find out which part of your program is causing the error.

    regards

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Segmentation fault (core dumped) Problem

    1. Very basically, it means your program is trying to access a memory area it is not suposed to.

    2. To the current directoy.

    3. You need to make sure your program doesn't do 1..
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,175
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Segmentation fault (core dumped) Problem

    The default behaviour on Ubuntu is that no core file is actually written, despite the message. If you have a core, you can examine it with a debugger to find out where it crashed. You can enable core dumps for your current terminal session by
    Code:
    ulimit -c unlimited

  6. #6
    Join Date
    Feb 2009
    Beans
    527
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Segmentation fault (core dumped) Problem

    Quote Originally Posted by mhaggard View Post
    1. What does that mean?
    About the same thing that "XYZ has encountered a problem and needs to close" or "XYZ has stopped working" usually does on Windows.

    A pointer in your program is pointing to memory that hasn't been malloc'ed yet, or you're trying to use a NULL pointer, or something of that sort.

    2. Where does it come from/ where is the core being 'dumped'?
    A core file (or core dump) is a file containing the complete memory image of a process that has crashed. The name is a holdover from the days when most computers had magnetic-core memory. It's generally written, as Bachstelze said, to the current directory, although nowadays, since most computer users aren't programmers anymore, the default settings of modern OS's generally have to be changed for a core dump to actually be written after a crash (for non-programmers, core dumps just end up being extra files cluttering the filesystem that they don't know what to do with).

    3. How do I fix that problem? or can I even fix it?
    There's a bug somewhere in your code, most likely involving a pointer (the first order of business whenever you see "Segmentation fault" or "SIGSEGV" is to double-check all the pointers in your code). Without seeing your code, we can't say exactly what it is. To fix the segfault problem you have to find and fix the bug.

  7. #7
    Join Date
    Jun 2012
    Beans
    22

    Re: Segmentation fault (core dumped) Problem

    Thanks for the welcome. Everybody replied so fast. I appreciate y'alls input already. For what it's worth, here is my code. It works on my windows machine without a problem. It is unfinished btw, just a homework assignment for my c++ class. I can't find any bugs... anymore input would be amazing. You guys have been great so far.

    Code:
    #include <iostream> //every program has this.
    #include <cstdlib> //standard library.
    #include <ctime> //for random.
    #include <fstream> //for reading and writing to a file.
    #include <string>
    
    using namespace std;
    
    int main(){
      ifstream infile;
      ofstream outfile;
      
      int x;
      int word_length;
      
      string word_selection;
      string words[100];
      
      char letter_bank[100]; // holds the letters of the selected word.
      char play; // for continuation of the game. 
      
      cout << "Would you like to play hangman? (Enter y or n): ";
      cin >> play;
      
      if(play == 'y' || play == 'Y'){
      
    	infile.open("hangman_input.txt");
      
    	while(infile){
    		infile >> x; 
    		for (int i = 0; i < x; i++){
    			infile >> words[i];
    			}
    		}
    	
    	long  seed = time(NULL); // gets current time
    	srand(seed);
    	int random_num = rand() % x; // Computer's random selection
    	
    	cout << random_num << endl;	
    	cout << random_num << endl;
    	
    	word_selection = words[random_num]; // This is the word we will play hangman with.
    	
    	word_length = word_selection.length();
    	cout << word_selection << " is " << word_length << " letters long" << endl;
    	
    	
    	
    	
    	
    	// Below is the end of the YES option...
    	}
    	
    	
    	else{
    		cout << "Good bye." << endl;
    		}
    	
    	
    
    	cout << "This is the value for x: " << x <<  endl; 
      return 0;
      }

  8. #8
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,175
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Segmentation fault (core dumped) Problem

    What happens if x > 100?

  9. #9

    Re: Segmentation fault (core dumped) Problem

    I was going to complain about your indentation, but then I noticed you're using Banner style pretty consistently. Props for consistency I guess, but it's still hard to read. At least line up the "else" with its corresponding "if".

  10. #10
    Join Date
    Jun 2012
    Beans
    22

    Re: Segmentation fault (core dumped) Problem

    Quote Originally Posted by spjackson View Post
    What happens if x > 100?
    if x gets bigger than 100 hundred, it will just change to that number. In the instructions, we aren't supposed to have more than 100 words anyways. Nevertheless, that number just changes to what ever it needs to be. Can you declare an array without giving some number of items in that array?

    Originally posted by trent.josephsen
    I was going to complain about your indentation, but then I noticed you're using Banner style pretty consistently. Props for consistency I guess, but it's still hard to read. At least line up the "else" with its corresponding "if".
    Yea I do need to correct the else indentation. I didn't notice it. Thanks. How would you suggest I write it? For readability I mean... Thanks in advance.

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •