[RESOLVED] Segmentation Fault
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: [RESOLVED] Segmentation Fault

  1. #1
    Join Date
    Aug 2010
    Posts
    16

    [RESOLVED] Segmentation Fault

    Hopefully this should be an easy problem I have looked all over my code and I cannot find where the problem lies. The program reads to large ints from a text file adds them and then outputs the result. Ive included the code below.

    Code:
    int main()
    {
    
            bigint a, b, c;
            std::ofstream outfile("results.txt");
            std::ifstream infile("data1-1.txt");
            if(infile.is_open())
            {
                    std::cout << "file open \n";
                    while (infile >> a)
                    {
                            infile >> b;
                            std::cout << a;
                            std::cout << b;
                            std::cout << "+__________________________________________\n";
                            c = a + b;
                            std::cout << c;
                    }
            }
            else std::cout << "Unable to open file\n";
    
            outfile.close();
            infile.close();
            return 0;
    }
    and here is the file it is reading from

    Code:
    1123215504550654654648632143546546440808844113546654696870324465406587305476857035744424554321649523187432184732416847321;
    
    3216654832155555151321;
    
    654448798765213211154654521035421556543324432153214463;
    
    65444068465406540065400666554448779873544321115674877635413246543214;
    This is a simple ADT exercise the input operator will read until the semicolon. The program works fine without reading from the file in a loop, but that is a requirement for this assignment.

    Any help would be greatly appreciated
    Last edited by originalmoose; September 29th, 2010 at 06:08 PM. Reason: resolved

  2. #2
    Join Date
    Aug 2010
    Posts
    16

    Re: Segmentation Fault

    I almost forgot here is the output when the program is executed:

    Code:
    112321554556546546486321435465464488844113546654696873244654
    65873547685735744424554321649523187432184732416847321
    
    3216654832155555151321
    
    +__________________________________________
    123215545565465464863214354654644888441135466546968732446546
    5873547685735744424554321649526404087016887971998642
    
    
    65444879876521321115465452135421556543324432153214463
    
    654446846546546546665544487798735443211156748776354132465432
    14
    
    +__________________________________________
    544468472009953454307576989533899645653723142095984539975767
    7
    
    Segmentation fault
    Last edited by originalmoose; September 29th, 2010 at 05:10 PM.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Segmentation Fault

    What is bigint? What provision does its operator>> make for semicolons? If none, you'll need to get the semicolon out of the file stream before you read the next value. Also, your couts should probably be separated by a newline.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Segmentation Fault

    Quote Originally Posted by originalmoose View Post
    Hopefully this should be an easy problem I have looked all over my code and I cannot find where the problem lies. The program reads to large ints from a text file adds them and then outputs the result. Ive included the code below.
    You didn't include the most important part -- what is "bigint"? Please post this class, as it is being used both as input and for addition.

    Secondly, please use code tags when posting code.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2010
    Posts
    16

    Re: Segmentation Fault

    here is the implementation for >> and operator+ for bigint

    Code:
    std::istream& operator>> (std::istream& in, bigint& num)
    {
            num.initialize();
            char ch;
            in >> ch;
            while (ch != ';')
            {
                    num.push_back(ch-'0');
                    in >> ch;
            }
            return in;
    }
    
    
    bigint bigint::operator+ (bigint rhs) const
    {
            bigint result;
            int rhs_max = rhs.size();
            int lhs_max = size();
            int max;
            if (rhs_max > lhs_max)
                    max = rhs_max;
            else
                    max = lhs_max;
            int idx = 0;
            int temp;
            while (idx < max)
            {
                    temp = digits[idx] + rhs.digits[idx] + result.digits[idx];
                    int i = 0;
                    while (temp != 0)
                    {
                            result.digits[idx+i] = (temp&#37;10);
                            temp = (temp/10);
                            ++i;
                    }
            ++idx;
            }
            return result;
    }
    I appologize for not using the code tags (new here) and its been a very long week so im a little out of it
    Last edited by originalmoose; September 29th, 2010 at 05:10 PM.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Segmentation Fault

    Quote Originally Posted by originalmoose View Post
    here is the implementation for >> and operator+ for bigint
    Can you also attach the input file you're using?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Aug 2010
    Posts
    16

    Re: Segmentation Fault

    Quote Originally Posted by Paul McKenzie View Post
    Can you also attach the input file you're using?

    Regards,

    Paul McKenzie
    Sure thing.
    Attached Files Attached Files

  8. #8
    Join Date
    Aug 2010
    Posts
    16

    Re: Segmentation Fault

    this will probably just be easier if I attach my files.
    Attached Files Attached Files

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Segmentation Fault

    The problem likely lies in incorrect logic within the bigint class. You haven't shown us enough of it to be certain, but offhand, I am suspicious because:

    1) The operator>> appears to be reading digits in most-to-least significant order, but operator+ appears to be proceeding in the same direction. When doing addition, you should be adding the least significant digits first. I say "appears" because I don't have enough information yet to be certain this is what's happening.

    2) You do not appear to be specifying how many digits "result" should have anywhere, which could very well lead to a segfault depending on the data type of "digits".

  10. #10
    Join Date
    Aug 2010
    Posts
    16

    Re: Segmentation Fault

    If I change the main program to this:

    Code:
    int main()
    {
    
            bigint a, b, c;
            std::string file_name = "data1-1.txt";
            std::ofstream outfile("results.txt");
            std::ifstream infile("data1-1.txt");
            if(infile.is_open())
            {
                            infile >> a >> b;
                            std::cout << a;
                            std::cout << b;
                            std::cout << "+__________________________________________\n";
                            c = a + b;
                            std::cout << c;
                            infile >> a >> b;
                            std::cout << a;
                            std::cout << b;
                            std::cout << "+__________________________________________\n";
                            c = a + b;
                            std::cout << c;
            }
            else std::cout << "Unable to open file: " << file_name << '\n';
            outfile.close();
            infile.close();
            return 0;
    }
    Everything works fine and I dont get a segmentation fault.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Segmentation Fault

    Quote Originally Posted by originalmoose View Post
    If I change the main program to this:
    First, did you test the bigint class with hard-coded values?

    Second, you know that if you're file reading fails, that program fails regardless of whether your bigint addition works. When I run your original program in Visual Studio 2010, the file reading goes haywire. Haywire meaning this line:
    Code:
    		while (infile >> a)
    goes into an infinite loop after two iterations.

    Maybe you should just hard-code those values into strings, and just call c = a+b twice to make sure it isn't bigint addition that is causing the problem first.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Aug 2010
    Posts
    16

    Re: Segmentation Fault

    Ive hardcoded the values in without any issues(well none that i didnt fix).

    I can also read in two sets of values for a and b and calculate c and repeat the same for two new values for a and b and calculate c again without any problem as long as im not running the input in a loop.
    Last edited by originalmoose; September 29th, 2010 at 05:37 PM. Reason: clarification

  13. #13
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Segmentation Fault

    Code:
    std::istream& operator>> (std::istream& in, bigint& num)
    {
    	num.initialize();
    	char ch;
    	in >> ch;  // what if in is invalid here?
    	while (ch != ';')  
    	{
    		num.push_back(ch-'0');
    		in >> ch;  // what if in becomes invalid here?
    	}
    	return in;
    }
    You do no check to see if the stream is in a valid state. You assume it is, and you keep reading.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 29th, 2010 at 05:48 PM.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Segmentation Fault

    Your while() condition must check for "in" being valid, not just assume it will be valid and only rely on semicolon for termination.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Aug 2010
    Posts
    16

    Re: Segmentation Fault

    Quote Originally Posted by Paul McKenzie View Post
    Your while() condition must check for "in" being valid, not just assume it will be valid and only rely on semicolon for termination.

    Regards,

    Paul McKenzie
    Thanks a bunch, I added in a test to make sure the in is valid and that fixed the issue.

    You guys have always been so helpful on this site, as someone learning to program I really appreciate it

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center