开发者

Segmentation Fault when outputting in C++

开发者 https://www.devze.com 2022-12-21 06:53 出处:网络
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

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 Larg开发者_运维技巧eInt::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);
}


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

return out;

With this line it works perfectly.


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


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.


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


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消