【问题标题】:Segmentation fault while pushing elements to a queue将元素推送到队列时出现分段错误
【发布时间】:2011-10-08 17:53:20
【问题描述】:

我在 Cpp 编程很久了,卡在一个地方。代码如下(较长,请耐心等待):

bool mazerouter( int xs, int ys, int ps, int xt, int yt, int pt )
{
    int n = gn; int w = gw;
    // The expansion list as maintained by the maze router. Could be declared as a class variable
    // but then, we need to find a way to empty the queue every time mazerouter is exited.
     queue <Wire_seg> explist;

    // Step 1: mark all the track segments that are available and adjacent to target as "t".
    // this depends on pin number.
     vector <  vector<int> > target = getWireSegments( xt, yt, pt );
    //int skip = 1/fc_in;
    int i,j;

    std::vector < std::vector <Wire_seg> > HorzWire = fpga.getHorzWire();
    std::vector < std::vector <Wire_seg> > VertWire = fpga.getVertWire();       
    for ( i = 0; i < target.size(); i = i+2 )
    {
        if ( pt == 1 || pt == 3 )
        {
            if ( VertWire[ target[i][0]][ target[i][1] ].getTag() == "a" )
                VertWire[ target[i][0] ][ target[i][1] ].setTag("t");
        }
        else if ( pt == 2 )
        {
            if ( HorzWire[ target[i][0]][ target[i][1] ].getTag() == "a" )
                HorzWire[ target[i][0] ][ target[i][1] ].setTag("t");
        }
    } cout<<"Completed routing step 1\n";

    // Step 2: mark all the track segments that are available and adjacent to source as "o". 
    // Push all the wire_segs in expansion list.
     vector <  vector<int> > source = getWireSegments( xs, ys, ps );
    //skip = 1/fc_out;
    len = 0; // for initialization of the source wire segments
    for ( i = 0; i < source.size(); i = i+1 )
    {
        if ( HorzWire[ source[i][0] ][ source[i][1] ].getTag() == "a" )
        {
            HorzWire[ source[i][0] ][ source[i][1] ].setNtag( 0 );
            HorzWire[ source[i][0] ][ source[i][1] ].setTag( "u" );
            explist.push( HorzWire[ source[i][0] ][ source[i][1] ] );
        }
    } cout<<"Completed routing step 2 and expansion list size: "<<explist.size()<<"\n";

    // The while loop

    bool pathfound = false;     
     vector <  vector <int> > h;
     vector <  vector <int> > v;
    while ( !explist.empty() )
    {
        Wire_seg ws = explist.front();
         cout<<"Completed routing step 3 substep 1 with front's tag:"<<explist.front().getTag()<<"\n";
        explist.pop();
        h = ws.getHorzCon();
        v = ws.getVertCon();
        len = ws.getNtag() + 1;
        int a;
        for ( a = 0; a < h.size(); a++ )
        {
            if ( HorzWire[ h[a][0] ][ h[a][1] ].getTag() == "t" )
            {
                 cout<<"target hit in horzwire\n";
                pathfound = true; 

                //path.push_back(  vector <  string > () ); pl++;   
                //path.at(pl).push_back( fpga.getHorzWire( h[a][0], h[a][1] ).getUid() );   
                break;
            }   
            else if ( HorzWire[ h[a][0] ][ h[a][1] ].getTag() == "a" )
            {
                HorzWire[ h[a][0] ][ h[a][1] ].setTag( "u" );HorzWire[ h[a][0] ][ h[a][1] ].setNtag( len );
                 cout<<"target not found, pushing horzwire("<<h[a][0]<<","<<h[a][1]<<") in explist with new tag:"<<HorzWire[ h[a][0] ][ h[a][1] ].getTag()<<"\n";
                explist.push( HorzWire[ h[a][0] ][ h[a][1] ] );
            }
        }
         cout<<"Completed routing step 3 substep 2\n";
        for ( a = 0; a < v.size(); a++ )
        {
            if ( VertWire[ v[a][0] ][ v[a][1] ].getTag() == "t" )
            {
                pathfound = true; 
                //path.push_back(  vector <  string > () ); pl++;   
                //path.at(pl).push_back( fpga.getVertWire( v[a][0], v[a][1] ).getUid() );
                break;
            }
            else if ( VertWire[ v[a][0] ][ v[a][1] ].getTag() == "a" )
            {
                VertWire[ v[a][0] ][ v[a][1] ].setTag( "u" ); VertWire[ v[a][0] ][ v[a][1] ].setNtag( len );
                                    // the following is the line causing trouble
                explist.push( VertWire[ v[a][0] ][ v[a][1] ] ); <=================================================================
                cout<<"target not found, pushing vertwire("<<v[a][0]<<","<<v[a][1]<<") in explist with new tag:"<<VertWire[ v[a][0] ][ v[a][1] ].getTag()<<"\n";
            }
        }
        h.clear();
        v.clear();
         cout<<"Completed routing step 3 substep 3 and expansion list size: "<<explist.size()<<"\n";
    }// end while
     cout<<"Completed routing step 3\n";
    return pathfound;
}// end mazerouter

我在 'explist.push( VertWire[ v[a][0] ][ v[a][1] ] );" 行中遇到分段错误,该行已被标记为 "

这个函数的作用:给定源块(xs,ys)和传入方向(ps,有四个值分别对应东、西、北、南)和目标块(xs,ys)和传出方向(pt,相同作为ps),我需要找到连接它们的路线。实际上,我在 FPGA 中实现路由,其中​​ Wire_seg 是逻辑块之间的线段类(实现为 Logic_Block 类),我需要在逻辑块之间进行路由。

就 'VertWire[ v[a][0] ][ v[a][1] ]' 的打印值而言,它是一个类,因此我正在访问它的成员变量标签以确保它存在。

@daniel:我已经在使用 g++ 编译器了。

“Wire_seg.h”的代码是:

class Wire_seg
{
// the track number for each wire segment, varies from 0 to w-1.
int tno;

// the orientation of the wire segment. 
char ornt;      

// vector to store the indices of all possible connections that can be made from
// this wire segment. 
std::vector < std::vector<int> > horz_con;
std::vector < std::vector<int> > vert_con;

//an unique id given to each wire of the following format "ornt:row:col"
std::string uid;

// The tag that helps determine whether this wire is availabel for a path or not.
// initially all wires are available.
std::string tag;

int ntag;

 public:    

Wire_seg()
{
    tag = "a";
}

    void setHorzCon( std::vector < std::vector<int> > h )
{
    horz_con = h;
}

void setVertCon( std::vector < std::vector<int> > v )
{
    vert_con = v;
}

void setTno( int t )
{
    tno = t;
}

void setOrnt( char orientation )
{
    ornt = orientation;     
}

void setUid( std::string id )
{
    uid = id;
}

void setTag( std::string t )
{
    tag.assign(t);
}

void setNtag( int t )
{
    ntag = t;
}

int getTno()
{
    return tno;
}

char getOrnt()
{
    return ornt;    
}

std::vector < std::vector<int> > getHorzCon()
{
    return horz_con;
}

std::vector < std::vector<int> > getVertCon()
{
    return vert_con;
}

std::string getUid()
{
    return uid;
}

std::string getTag()
{
    return tag;
}

int getNtag()
{
    return ntag;
}
};

我希望这能让问题更清楚。

【问题讨论】:

  • 你能解释一下这个函数的作用吗?看起来您正在尝试使用广度优先搜索来解决迷宫问题,这是正确的吗?什么是线变量?
  • 您是否尝试过打印出VertWire[ v[a][0] ][ v[a][1] ] 的值。如果它崩溃了,您就知道您正在访问超出范围的数组,从而导致了 UB。
  • 请张贴Wire_seg的代码,特别是。构造函数/析构函数。
  • 这段代码摘录不完整,我不敢相信它是最小的。

标签: c++ queue segmentation-fault


【解决方案1】:

您对std::deque 的使用看起来很安全,因此如果您遇到分段错误,您可能正在使用不执行边界检查的std::vector 实现进行编译。如果可以的话,打开它。

如果您的环境不提供边界检查,请尝试使用 g++(提供)。或者围绕std::vector 编写一个简单的包装类,为您提供所需的接口:size() 和operator[] 似乎就是您所需要的。你自己的边界检查也是如此。然后,您将能够看到您在向量末尾之后访问的位置。

【讨论】:

  • 刚才说的你是怎么做的?
猜你喜欢
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 2020-11-15
  • 2021-12-03
  • 2021-10-17
  • 1970-01-01
  • 2020-11-15
  • 2016-02-04
相关资源
最近更新 更多