2

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);
}
Josh Curren
  • 10,171
  • 17
  • 62
  • 73
  • what is the type of `l`? – Vlad Feb 21 '10 at 23:02
  • Could You describe more? What type is of l? What is l.usedLength, l.largeInt? – lollinus Feb 21 '10 at 23:03
  • 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 – Josh Curren Feb 21 '10 at 23:05
  • i added the code for the whole class – Josh Curren Feb 21 '10 at 23:08
  • More code would be nice. It's hard to find a bug with just these lines. – Javier Feb 21 '10 at 23:09
  • Could you please post the rest of the program? How did the `l` get its value? – Vlad Feb 21 '10 at 23:14
  • l got its value from cin >> ... I have been using 12345 to test with – Josh Curren Feb 21 '10 at 23:21
  • Okay. In which iteration does it crash? What is output? Do you see `In for loop` at the program's output? – Vlad Feb 21 '10 at 23:25
  • You seem to be missing a copy ctor & assignment operator and have pointer members (largeInt). Are you making copies of your LargeInt objects? Also you'll at least want to use totalLength in operator>> to make sure you're not writing outside the bounds largeInt. – Eugen Constantin Dinca Feb 21 '10 at 23:26
  • if i enter 12345 it gets to the for loop and then has the seg fault... when i uncomment the text in the loop i can get it to print 1234 and then the seg fault.. if i uncomment both of the texts it has no seg fault – Josh Curren Feb 21 '10 at 23:31
  • 1
    See my answer at the bottom :-) – Vlad Feb 21 '10 at 23:53

8 Answers8

4

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

return out;

With this line it works perfectly.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • Ahh Yes of course someone forgot to read compiler output and never ignore warnings. How could I miss that. – lollinus Feb 22 '10 at 23:49
  • 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? – Macke Feb 23 '10 at 17:28
  • Actually, VS must issue a warning at warning level > 0. See http://msdn.microsoft.com/en-us/library/ft5xye74.aspx – Vlad Feb 23 '10 at 17:39
1

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

Macke
  • 24,812
  • 7
  • 82
  • 118
1

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
  • 101,701
  • 37
  • 181
  • 258
0

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
  • 43,810
  • 29
  • 138
  • 169
0

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
  • 11,819
  • 2
  • 39
  • 40
  • You need that bounds check when incrementing usedLength. Otherwise, as soon as you read that 51st digit, you're scribbling all over memory. – crazyscot Feb 21 '10 at 23:18
  • 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 Feb 21 '10 at 23:28
0

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
  • 2,332
  • 1
  • 13
  • 25
0

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

lollinus
  • 434
  • 4
  • 11
0

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.

kokosing
  • 5,251
  • 5
  • 37
  • 50
  • I'm not in linux... im on Win 7 – Josh Curren Feb 21 '10 at 23:20
  • 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 – kokosing Feb 21 '10 at 23:26
  • http://www.microsoft.com/downloads/details.aspx?familyid=c4a25ab9-649d-4a1b-b4a7-c9d8b095df18&displaylang=en maybe it will be helpful – kokosing Feb 21 '10 at 23:29