Josh Curren
Josh Curren

Reputation: 10226

Segmentation Fault when outputting in C++

I am trying to print out an array of integers. I am getting a seg fault when I try to print as below. If I uncomment the "In for loop" it will print everything except the last item of the array and it still has a seg fault. When I uncomment both of the comments (or just the "done with for loop") everything prints fine. Why does this happen and how do I fix it?

for( int i = 0; i < l.usedLength; i++ )
{
    //cout << "**********In for loop" << endl;
    cout << l.largeInt[ i ];
}
//cout << "**********done with for loop" << endl;

Here is the whole class:

#include "LargeInt.h"
#include <ctype.h>

LargeInt::LargeInt()
{
    usedLength = 0;
    totalLength = 50;

    largeInt = new int[totalLength];
    for( int i=0; i<totalLength; i++ )
    {
        largeInt[i] = 0;
    }
}

LargeInt LargeInt::operator+(const LargeInt &l) const
{}

LargeInt LargeInt::operator-(const LargeInt &l) const
{}

LargeInt LargeInt::operator*(const LargeInt &l) const
{}

LargeInt LargeInt::operator/(const LargeInt &l) const
{}

bool LargeInt::operator==(const LargeInt &l) const
{}

ostream& operator<<(ostream &out, const LargeInt &l)
{
    cout << "In output" << endl;

    if( l.usedLength == 0 )
    {
        cout << 0;
    }
    else
    {
        cout << "In else... NON 0" << endl;

        for( int i = 0; i < l.usedLength; i++ )
        {
            cout << "In for loop" << endl;
            cout << l.largeInt[ i ];
        }
        //cout << "done with for loop" << endl;
    }
    //cout << "after the if.... all done with output" << endl;
}

istream& operator>>(istream &in, LargeInt &l)
{
    char x;
    while (std::cin.get(x) && x >= '0' && x <= '9')
    {
        l.largeInt[ l.usedLength ] = x-48;
        l.usedLength++;
        //need to check array length and make bigger if needed
    }

}

Main:

#include <stdlib.h>
#include <iostream>

#include "LargeInt.h"

int main(int argc, char** argv) {

    cout << "\nJosh Curren's Assignment #5 - Large Integer\n" << endl;

    LargeInt lint;

    cout << "Enter a large int: ";
    cin >> lint;

    cout << "\nYou entered: " << endl;
    cout << lint << endl;
    cout << endl;


    return (EXIT_SUCCESS);
}

Upvotes: 2

Views: 292

Answers (8)

Vlad
Vlad

Reputation: 35594

You forgot the last line in ostream& operator<<(ostream &out, const LargeInt &l):

return out;

With this line it works perfectly.

Upvotes: 4

James Curran
James Curran

Reputation: 103605

Well, that code shouldn't even compile: Your operators don't return anything. Further, you have out & in as parameters, but use cout & cin. But none of those would cause the problem you are seeing.

Since the crash moves depending on the presence or absence of code and strings nearby, I'll up my Psychic Debugging Skills, and say that some where, you are overrunning the end of an array, presumably largeInt.

Also, it would be nice to see the main() function of your code.

Upvotes: 1

Macke
Macke

Reputation: 25710

You should set usedLength to zero at the start of the istream operator >>.

Upvotes: 1

kokosing
kokosing

Reputation: 5601

If you are using linux (or something similar) forget about stdout to find segfault. Try to use valgring or generate core and analise it with gdb. Because of buffering streams when segfault occurs there is no guaranty that your print will even appear.

Upvotes: 0

lollinus
lollinus

Reputation: 434

I see no destructor I Your code. I can guess that delete largeInt may be used wrong.

Upvotes: 0

David Harris
David Harris

Reputation: 2350

My guess is that usedLength is the size of the array and the last valid index is usedLength - 1. So when you access the element at usedLength you get the seg fault.

Upvotes: 0

Tom
Tom

Reputation: 45174

My bet is that the problem is in the l.largeInt [i] part, but can't tell without further info.

COuld you post more code?

l is a LargeInt which is the class this code is in... l has an array of 50 ints which is largeInt. l also has an int usedLength which is the number of items in the array

Check that usedLength < 50.

And do some debugging. Looks like the kind of problem where a debugger would go a long way (aren't the -almost- all?)

Upvotes: 0

crazyscot
crazyscot

Reputation: 12019

What you're saying doesn't, on the face of it, appear to make sense; sending stuff to stdout shouldn't affect whether or not the code segfaults. My guess would be that you've got some subtle memory corruption bug elsewhere, possibly in l (whatever type it is - you haven't said much about it).

Upvotes: 0

Related Questions