what is segmentation fault?

This is a discussion on what is segmentation fault? within the C++ Programming forums, part of the General Programming Boards category; What does segmentation fault mean? Why when i run the following code, there is a segmentation fault?: Code: #include <string.h> ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    Question what is segmentation fault?

    What does segmentation fault mean?

    Why when i run the following code, there is a segmentation fault?:

    Code:
    #include <string.h>
    #include<iostream.h>
    #include<fstream.h>
    #include<ctype.h>
    #include"lvp/vector.h"
    int main()
    {
    //      const int BUFF_SIZE=100000;
            const long long BUFF_SIZE = 1000000;
            int a;
            ifstream infile;
            ofstream outfile;
            char buff[BUFF_SIZE];
    // open the files
    //      infile.open("/misc/tmp/kddcup.data_10_percent.fresh");
            infile.open("data.txt");
            outfile.open("output.txt");
    // make sure the files are open
            if(!infile.is_open()){
                    cerr << "error opening input file";
                    return 1;
            }
            if(!outfile.is_open()){
                    cerr << "error opening output file";
                    return 1;
            }
    // loop until the end of the input file
            for(a=0; !infile.eof();){
    // read in one line
                    char *p;
                    char word[20] = "";
                    infile.getline(buff,200);
    //              infile.ignore(1);
                    p = strtok(buff,",");
                    for(int i=0; i<42; i++)
                    {
                            if(i==1)
                            {
                                    strcpy(word,p);
                                    cout<<word<<endl;
                                    outfile << word << endl;
                            }
                            if(i==2)
                            {
                                    strcpy(word,p);
                                    cout<<word<<endl;
                                    outfile << word << endl;
                            }
                            if(i==3)
                            {
                                    strcpy(word,p);
                                    cout<<word<<endl;
                                    outfile << word << endl;
                            }
                            if(i==41)
                            {
                                    strcpy(word,p);
                                    cout<<word<<endl;
                                    outfile << word << endl;
                            }
                            p = strtok(NULL,",");
                    }
            }
    
    // close the files
            infile.close();
            outfile.close();
    // pause so you can see the output on the screen
            cout << " **** All done! ****\n";
            cin.get();
            return 0;
    }
    Thanks for response,

    ~D
    Last edited by MKashlev; 08-06-2002 at 07:00 PM.
    Dmitry Kashlev

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thumbs up I hope this helps.

    segmentation fault n.

    [Unix] 1. [techspeak] An error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers in the source code, dereferencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer. The classic example is:

    int i;
    scanf ("%d", i); /* should have used &i */

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >p = strtok(buff,",");
    >p = strtok(NULL,",");
    strtok will return NULL when it doesn't find what it's looking for. You must check for p not being null before you attempt to use it.
    This could well be the cause of your problem.

    >strcpy(word,p);
    Try using strncpy() to be on the safe side.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Another thought, though I'm not pre-empting correlcj and Hammer. (Actually, I like their possible solutions. ).

    Try decreasing BUFF_SIZE and see if your code will compile.

    My compilers (Borland) tend to be more specific (if sometimes indecipherable) with their error messages.

    Actually, it appears that you're attempting to create arrays that are too large. Borland allows up to 64K (140K when the "modifier" huge is used).
    Code:
    int huge arr[70000L];  /* Allocate 140,000 bytes */
    You're at 1Mb (almost). Just a thought.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also you might try changing this:
    Code:
    // loop until the end of the input file
            for(a=0; !infile.eof();){
    // read in one line
                    char *p;
                    char word[20] = "";
                    infile.getline(buff,200);
    To this:
    Code:
    // loop until the end of the input file
            while (infile.getline(buff,200)){
    // read in one line
                    char *p;
                    char word[20] = "";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21