代码之家  ›  专栏  ›  技术社区  ›  Josh Curren

C++中输出时的分割错误

c++
  •  2
  • Josh Curren  · 技术社区  · 16 年前

    我正在打印一个整数数组。我得到一个seg错误,当我试图打印如下。如果我取消注释“In-for-loop”,它将打印除数组最后一项之外的所有内容,并且它仍然有一个seg错误。当我取消对这两个注释的注释(或者仅仅是“donewithfor循环”)时,一切都很好。为什么会发生这种情况?我该如何解决?

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

    以下是全班同学:

    #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
        }
    
    }
    

    主要内容:

    #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);
    }
    
    8 回复  |  直到 16 年前
        1
  •  4
  •   Vlad    16 年前

    你忘了最后一行了 ostream& operator<<(ostream &out, const LargeInt &l) :

    return out;
    

        2
  •  1
  •   Macke    16 年前

    usedLength 在istream开始时归零 operator >>

        3
  •  1
  •   James Curran    16 年前

    好吧,这些代码甚至不应该编译:操作符不返回任何东西。此外,您还有;在中作为参数,但使用cout&中国。但这些都不会引起你所看到的问题。

    由于崩溃的移动取决于附近是否存在代码和字符串,因此我将提高我的心理调试技能,并说在某些情况下,您可能正在溢出数组的末尾 largeInt .

        4
  •  0
  •   crazyscot    16 年前

    从表面上看,你说的似乎没有道理;向stdout发送内容不应该影响代码是否出错。我的猜测是,你在其他地方有一些微妙的内存损坏错误,可能是在l中(不管它是什么类型-你没有说太多)。

        5
  •  0
  •   Tom    16 年前

    我敢打赌问题出在l.largeInt(我)那部分,但如果没有进一步的信息就说不出来。

    你能多发些代码吗?

    我是一个大人物 此代码位于。。。我有一系列的 一个int usedLength,它是一个数字 数组中的项

    做一些调试。看起来像是调试器需要很长时间才能解决的问题(难道不是所有的问题吗?)

        6
  •  0
  •   David Harris    16 年前

    我的猜测是usedLength是数组的大小,最后一个有效索引是usedLength-1。因此,当访问usedLength处的元素时,会出现seg错误。

        7
  •  0
  •   lollinus    16 年前

    我能猜到 delete largeInt

        8
  •  0
  •   kokosing    16 年前

    如果您使用的是linux(或类似的东西),请忘记stdout来查找segfault。尝试使用valgring或生成core并用gdb进行分析。由于segfault发生时缓冲了流,因此无法保证您的打印会出现。