views:

103

answers:

8

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);
}
A: 

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?)

Tom
i added the whole class
Josh Curren
i have only tested with < 10 items so far so its not that usedLength is > 50
Josh Curren
A: 

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).

crazyscot
i added the whole class
Josh Curren
You need that bounds check when incrementing usedLength. Otherwise, as soon as you read that 51st digit, you're scribbling all over memory.
crazyscot
right now i have been testing with < 50 digits... I will eventually add some code to the input that will make a larger array if it gets to 50 so usedLength will never be > the totalLength
Josh Curren
A: 

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.

David Harris
it is never accessing the item at usedLength.
Josh Curren
Your posted code confirms this.
David Harris
where do you see this?
Josh Curren
My mistake; Vlad has the correct answer.
David Harris
A: 

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

lollinus
A: 

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.

kogut
I'm not in linux... im on Win 7
Josh Curren
http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows check this thread, here you will find some windows tools for verifing your usage of memory
kogut
kogut
+1  A: 

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

Marcus Lindblom
Thanks! But that doesnt fix the seg fault.
Josh Curren
+1  A: 

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.

James Curran
I added the code for main
Josh Curren
+4  A: 

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

return out;

With this line it works perfectly.

Vlad
Ahh Yes of course someone forgot to read compiler output and never ignore warnings.How could I miss that.
lollinus
I'm curious as to why you don't get this as an error sometimes . It's bitten me before. Is it a VS2008 compiler bug or a dark corner of the language?
Marcus Lindblom
Actually, VS must issue a warning at warning level > 0. See http://msdn.microsoft.com/en-us/library/ft5xye74.aspx
Vlad