c++ std::vector 孤儿范围错误

发布于 2024-09-29 02:34:12 字数 1493 浏览 1 评论 0原文

处理图(来自图论)表示和转换的程序。邻接列表和矩阵的实现就像向量的动态数组(不要问为什么不是向量的向量),以下函数程序退出时出现内存错误,编译器指向孤儿向量定义。

int vertex,edges;
vector<int> *adjacencyList,*adjacencyMatrix;

void listToAdMatrix(int vertexNumber, vector<int> *List, vector<int> *Matrix){
 int in=0,cont=0;
 for(int i=0;i<vertexNumber;i++){
  in=i;
  for(auto j=List[in].begin();j!=List[in].end();j++){
   for(int k=0;k<vertexNumber;++k){
    if(k==*j) Matrix[cont].push_back(1); 
    else Matrix[cont].push_back(0);
   }
   cont++;
  }
 }
}

//function call
//vertex(number) and ajacencyList are initialized

adjacencyMatrix=new vector<int>[vertex];
listToAdMatrix(vertex,adjacencyList,adjacencyMatrix);

STL 中编译器指向的“错误源”:

http://i51.tinypic.com/2dt0t9e.jpg


错误消息:

graph.exe 中 0x001a543b 处未处理的异常:0xC0000005:读取位置 0xfdfdfe01 时发生访问冲突。


用于填充邻接列表的 fillList 函数:

void fillList(int vertexNumber, vector<int> *List){
    int input=0;
    for (int i=0;i<vertexNumber;i++){   
        int in=i;
        cout<<"Introduce adjacent vertexes for the vertex -"<<i+1<<"-:"<<endl;
        for(int j=0;j<vertexNumber;j++){
            std::cout<<i+1<<"-";
            std::cin>>input;
            if(input==0) break;
            List[i].push_back(input-1);
        }
    }
}

欢迎提供任何线索。


A program dealing with graphs(from graph theory) representation and transformation.The adjacency list and matrix are implemented like dynamic arrays of vectors(don't ask why not vector of vector) for the following function program exits with memory error and compiler pointing to the orphan vector definition.

int vertex,edges;
vector<int> *adjacencyList,*adjacencyMatrix;

void listToAdMatrix(int vertexNumber, vector<int> *List, vector<int> *Matrix){
 int in=0,cont=0;
 for(int i=0;i<vertexNumber;i++){
  in=i;
  for(auto j=List[in].begin();j!=List[in].end();j++){
   for(int k=0;k<vertexNumber;++k){
    if(k==*j) Matrix[cont].push_back(1); 
    else Matrix[cont].push_back(0);
   }
   cont++;
  }
 }
}

//function call
//vertex(number) and ajacencyList are initialized

adjacencyMatrix=new vector<int>[vertex];
listToAdMatrix(vertex,adjacencyList,adjacencyMatrix);

The "source of error" in STL where compiler points:

http://i51.tinypic.com/2dt0t9e.jpg


The error message:

Unhandled exception at 0x001a543b in graph.exe: 0xC0000005: Access violation reading location 0xfdfdfe01.


the fillList function used to fill the adjacency list :

void fillList(int vertexNumber, vector<int> *List){
    int input=0;
    for (int i=0;i<vertexNumber;i++){   
        int in=i;
        cout<<"Introduce adjacent vertexes for the vertex -"<<i+1<<"-:"<<endl;
        for(int j=0;j<vertexNumber;j++){
            std::cout<<i+1<<"-";
            std::cin>>input;
            if(input==0) break;
            List[i].push_back(input-1);
        }
    }
}

Any clue is welcome.


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场春暖 2024-10-06 02:34:12

一个问题是您尚未初始化 adjacencyList。根据此代码,当它进入 listToAdMatrix 时,它应该指向垃圾(或 null)。当尝试索引到 List[in] 时,这可能会导致错误。

One problem is that you haven't initialized adjacencyList yet. According to this code, it should be pointing at garbage (or null) when it gets into listToAdMatrix. That sould cause an error when trying to index into List[in].

月下客 2024-10-06 02:34:12

您确定 adjacencyList[in].size() 始终小于顶点吗?将代码放在 j 循环之前进行检查。现在你只需要放置一条虚拟线和一个断点来看看它是否会发生。

如果是这样,则要么是 adjacencyList 初始化不正确,要么是您需要处理的情况。

您还需要确保 adjacencyList 设置为长度为 vertex 的数组,但这更明显,因此您可能正在这样做。

Are you sure that adjacencyList[in].size() is always less than vertex? Put code before the j loop to check for that. Right now you just need to put a dummy line and a break point to see if it can happen.

If it does, then either adjacencyList is initialized incorrectly or this is a situation that you need to handle.

You also need to make sure that adjacencyList is set to an array of length vertex, but that's more obvious, so you are probably doing that.

回忆躺在深渊里 2024-10-06 02:34:12

问题已解决:

我再次读取了数组之外的内容。错误出在我用来寻址矩阵(动态数组)中向量的cont变量中,当它超出数组时增加它。通过插入 while 语句解决了问题。谢谢大家的回答。

int vertex,edges;
vector<int> *adjacencyList,*adjacencyMatrix;

void listToAdMatrix(int vertexNumber, vector<int> *List, vector<int> *Matrix){
 int in=0,cont=0;
 for(int i=0;i<vertexNumber;i++){
  in=i;
  for(auto j=List[in].begin();j!=List[in].end();j++){
   while(cont!=vertexNumber){
     for(int k=0;k<vertexNumber;++k){
       if(k==*j) Matrix[cont].push_back(1); 
       else Matrix[cont].push_back(0);
     }
     cont++;
   }
  }
 }
}

//function call
//vertex(number) and ajacencyList are initialized

adjacencyMatrix=new vector<int>[vertex];
listToAdMatrix(vertex,adjacencyList,adjacencyMatrix);

Problem Solved :

Once again i've read outside of an array.The error was in the cont variable which i used to address the vectors in the Matrix(dynamic array), incrementing it while it got outside the array.Problem is solved by inserting a while statement.Thank you all for the answers.

int vertex,edges;
vector<int> *adjacencyList,*adjacencyMatrix;

void listToAdMatrix(int vertexNumber, vector<int> *List, vector<int> *Matrix){
 int in=0,cont=0;
 for(int i=0;i<vertexNumber;i++){
  in=i;
  for(auto j=List[in].begin();j!=List[in].end();j++){
   while(cont!=vertexNumber){
     for(int k=0;k<vertexNumber;++k){
       if(k==*j) Matrix[cont].push_back(1); 
       else Matrix[cont].push_back(0);
     }
     cont++;
   }
  }
 }
}

//function call
//vertex(number) and ajacencyList are initialized

adjacencyMatrix=new vector<int>[vertex];
listToAdMatrix(vertex,adjacencyList,adjacencyMatrix);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文