【问题标题】:Segmentation fault on accessing 2D vector [closed]访问二维向量时的分段错误[关闭]
【发布时间】:2014-04-19 21:44:43
【问题描述】:

我试图解决处理矩阵的问题,因为我使用了向量,但是当我尝试运行程序时它显示分段错误(核心转储)错误,并且在运行 gdb 时显示以下错误。

Program received signal SIGSEGV, Segmentation fault.
0x00000000004011b4 in main () at rectangles.cpp:43
if (p<A-1 && q <B-1 && M[p][q]==1 && M[p+1][q] == 1 && M[p][q+1]==1)

rectangles.cpp 是:

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    int A,B,notRect,numRect=0;
    bool rectInc=false;
    string line;

    freopen("input.txt","rt",stdin);
    freopen("output.txt","wt",stdout);
    A=cin.get()-'0';
    cin.get();
    B=cin.get() - '0';
    cin.get();
    vector<vector<int> > M(A);
    vector<vector<int> > topLeft(2);
    vector<vector<int> > topRight(2);
    vector<vector<int> > bottomLeft(2);
    vector<vector<int> > bottomRight(2);

    for (int i = 0; i < A; i++)
    {
        getline(cin,line);
        for (int k = 0; k < line.size(); k++)
        {
            if (line.at(k)!=' ')
            {
                M[i].push_back(line.at(k)-'0');
            }

        }
    }

    for (int p = 0; p < A; p++)
    {
        for (int q = 0; q < B; q++)
        {
            if (p<A-1 && q <B-1 && M[p][q]==1 && M[p+1][q] == 1 && M[p][q+1]==1)
            {
                topLeft[0].push_back(p);
                topLeft[1].push_back(q);
            }
            if (p<A-1 && q >0 && M[p][q]==1 && M[p+1][q] == 1 && M[p][q-1]==1)
            {
                topRight[0].push_back(p);
                topRight[1].push_back(q);
            }
            if (p>0 && q <B-1 && M[p][q]==1 && M[p-1][q] == 1 && M[p][q+1]==1)
            {
                bottomLeft[0].push_back(p);
                bottomLeft[1].push_back(q);
            }
            if (p>0 && q >0 && M[p][q]==1 && M[p-1][q] == 1 && M[p][q-1]==1)
            {
                bottomRight[0].push_back(p);
                bottomRight[1].push_back(q);
            }
        }   

    }

    for (int i = 0; i < topLeft[0].size(); i++)
    {
        for (int j = 0; j < topRight[0].size();j++)
        {
            for (int k = 0; k < bottomLeft[0].size(); k++)
            {
                for (int l = 0; l < bottomRight[0].size(); l++)
                {
                    notRect=0;
                    rectInc=false;
                    if (topLeft[0][i]==topRight[0][j] && topLeft[1][i]+1<topRight[1][j] && topLeft[1][i]==bottomLeft[1][k] && topLeft[0][i]+1<bottomLeft[0][k] && bottomLeft[0][k]==bottomRight[0][l] &&  bottomLeft[1][k]+1<bottomRight[1][l]&& topRight[1][j]==bottomRight[1][l]&& topRight[0][j]+1<bottomRight[0][l])
                    {
                        for (int p = topLeft[1][i]; p < topRight[1][j]; p++)
                        {
                            if (M[topLeft[0][i]][p]!=1)
                            {
                                notRect=1;
                            }
                        }
                        if (notRect==1)
                        {
                            break;
                        }
                        for (int p = topLeft[0][i]; p < bottomLeft[0][k]; p++)
                        {
                            if (M[p][topLeft[1][i]]!=1)
                            {
                                notRect=1;
                            }
                        }
                        if (notRect==1)
                        {
                            break;
                        }
                        for (int p = bottomLeft[1][k]; p < bottomRight[1][l]; p++)
                        {
                            if (M[bottomLeft[0][k]][p]!=1)
                            {
                                notRect=1;
                            }
                        }
                        if (notRect==1)
                        {
                            break;
                        }
                        for (int p = topRight[0][j]; p < bottomRight[0][l]; p++)
                        {
                            if (M[p][topRight[1][j]]!=1)
                            {
                                notRect=1;
                            }
                        }
                        if (notRect==1)
                        {
                            break;
                        }

                        for (int z =topLeft[0][i]+1 ; z <=bottomLeft[0][k]-1 ; z++)
                        {
                            for (int t = topLeft[1][i]+1; t <= topRight[1][j]-1 ; t++)
                            {
                                if (M[z][t]==0)
                                {
                                    numRect++;
                                    rectInc=true;
                                }
                                else
                                {
                                    rectInc=false;
                                }
                                if (rectInc)
                                {
                                    break;
                                }
                            }
                            if (rectInc)
                            {
                                break;
                            }

                        }
                        if (rectInc)
                        {
                            break;
                        }
                    }
                }
            }
        }
    }

    cout<<numRect;
    return 0;

}

而且使用的input.txt有这个文字

6;7
0001111
0111011
0101011
0101011
0111011
0001111

请帮助我理解错误并修复它。 谢谢。

【问题讨论】:

  • 这个问题似乎离题了,因为它缺乏足够的信息来诊断问题。更详细地描述您的问题或在问题本身中包含一个最小示例
  • 请先调试后再问这里...
  • 非常感谢您的建议,因为这是我在这里的第一个问题,我无法正确地提出问题,但我知道下次该怎么做,我已经找到了解决方案,谢谢。

标签: c++ vector segmentation-fault


【解决方案1】:

确保您的所有M[i] 的长度均为B。我假设,您的第一个 getline 只会得到一个空行(因为您可能没有跳过它。您使用的是 Windows 吗?那么换行符将是两个字符宽)。因此访问M[0][0] 元素已经越界了。

调试输出通常有助于找到类似的内容,并且将 M[i][j]M.at(i).at(j) 交换会提供更详细的信息(因为它会执行边界检查)。

【讨论】:

  • 确保 M[i] 的长度为 B 是不够的。代码访问M[0...A][0...B]
  • @Roberto 没有。 If 是惰性求值的,因此首先确保 p&lt;A-1 &amp;&amp; q &lt;B-1 。所以即使M[p+1][q] 也不会越界(如果向量创建正确的话)。
  • 对不起,你是对的,我的错
  • 非常感谢,是的,问题出在第一个 getline 上,实际上在 Windows Visual Studio 上它运行良好,但在 ubuntu 上它产生了问题,所以在我更新代码以正确获取线路后,它运行良好。
【解决方案2】:

更改以下代码:

int A,B,notRect,numRect=0;
bool rectInc=false;
string line; 
freopen("input.txt","rt",stdin);
freopen("output.txt","wt",stdout);
A=cin.get()-'0';
cin.get();
B=cin.get() - '0';
cin.get();

到:

int A=-1,B,notRect,numRect=0;
bool rectInc=false;
string line,temp;
freopen("input.txt","rt",stdin);
freopen("output.txt","wt",stdout);
getline(cin,line);
istringstream ss(line);
while (ss)
{
    if(getline(ss,temp,';'))
    {
        if(A==-1)
            A=atoi(temp.c_str());
        else
            B=atoi(temp.c_str());
    }
}

解决了,谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多