C++中申请动态二维数组不当造成程序异常退出的问题

开门见山

  1 #include "Graph.h"
  2 #include <Vertex.h>
  3 #include <Edge.h>
  4 #include <time.h>
  5 #include <cstdlib>
  6 #include <cstddef>
  7 #include <fstream>
  8 #include <string>
  9 using namespace std;
 10 void Graph::randGene_directed(const int &nbNodes){
 11 srand (time(NULL)+1);
 12     for(int i=0; i<nbNodes;i++){//initialize all the nodes/Vertexes and give them a color
 13         Vertex *v = new Vertex(i);// to initialize
 14         v->color = 0; //O black & 1 white
 15         listVertex.push_back(v);
 16     }
 17 
 18     int p=0;//index of the edges
 19     for(int i=0; i<nbNodes;i++){//to create the matrix
 20         for(int j=0; j<nbNodes;j++){
 21             int index =rand() % 2;//generate an random number between 0,1
 22             int weight=rand() % 100+1;
 23             if(index == 1){//if an edge exist
 24                 Edge *e = new Edge(p, listVertex[i],listVertex[j],weight);
 25                 matrix[i][j]=weight;
 26                 p++;
 27                 listEdges.push_back(e);
 28             }
 29         }
 30     }
 31 
 32 }
 33 void Graph::randGene_undirected(const int &nbNodes){
 34 srand (time(NULL)+1);
 35     for(int i=0; i<nbNodes;i++){//initialize all the nodes/Vertexes and give them a color
 36         Vertex *v = new Vertex(i);// to initialize
 37         v->color = 0; //O black & 1 white
 38         listVertex.push_back(v);
 39     }
 40     int p=0;//index of the edges
 41     for(int i=0; i<nbNodes;i++){//to create the matrix
 42         for(int j=0; j<nbNodes;j++){
 43             int index =rand() % 2;//generate an random number between 0,1
 44             int weight=rand() % 100+1;
 45             if(index == 1){//if an edge exist
 46                 Edge *e = new Edge(p, listVertex[i],listVertex[j],weight);
 47                 p++;
 48                 listEdges.push_back(e);
 49             }
 50         }
 51     }
 52 }
 53 Graph::Graph(int nbNodes)//construction of a directed graph with a randomly generated matrix
 54 {
 55     matrix=new int*[nbNodes];
 56     for(int i=0;i<nbNodes;i++){
 57         *matrix=new int[nbNodes];
 58     }
 59 
 60     for(int x=0; x<nbNodes;x++){
 61             for(int y=0; y<nbNodes;y++){
 62                 printf("
line %d column %d
%d",x,y,matrix[x][y]);
 63             }
 64     }
 65 
 66     randGene_directed(nbNodes);
 67     printf("test");
 68  for(int x=0; x<nbNodes;x++){
 69             for(int y=0; y<nbNodes;y++){
 70                printf("%d
",matrix[x][y]);
 71             }
 72     }
 73 }
 74 Graph::Graph(ifstream * of){
 75     string line,lines[3];
 76     //char ch[3];
 77     int i=0,Nb_Vertices=0;
 78     while(getline(*of,line)){
 79         lines[i]=line;
 80         if(i++>=3){
 81             printf("error:lines exceeded");
 82             return;
 83         }
 84     }
 85     if(Nb_Vertices=stoi(lines[0],NULL,0)<=0){
 86         printf("error:invalid vertex number");
 87         return;
 88         }
 89     if(lines[1]!="o"&&lines[1]!="n"){
 90         printf("error:invalid graph type");
 91         return;
 92         }
 93     if(lines[2]!="m"&&lines[2]!="l"){
 94         printf("error:invalid graph representation");
 95         return;
 96         }
 97     if(lines[1]=="o"){
 98         if(lines[2]=="l"){
 99            // Graph::Graph(Nb_Vertices);
100         }
101     }
102 
103 
104 
105 
106 }
107 
108 Graph::~Graph()
109 {
110     for(int i=0;i<listVertex.size();i++)
111         delete[] matrix[i];
112     delete[] matrix;
113 }
114 
115 void Graph::display(bool typeOfGraph,bool typeOfRepresentation){
116     ofstream myFeed("Files/Result.txt");//supprime l'ancien fichier ou en créé un nouveau
117     //display the number of vertexes
118     cout<<listVertex.size()<<endl;
119     myFeed<<listVertex.size()<<endl;
120     if(typeOfGraph){//Type of graph : directed graph
121         cout<<"o"<<endl;
122         myFeed<<"o"<<endl;
123     }else{//Type of graph : undirected graph
124         cout<<"n"<<endl;
125         myFeed<<"n"<<endl;
126     }
127     if(typeOfRepresentation){//type of representation : matrix
128         cout<<"m"<<endl;
129         myFeed<<"m"<<endl;
130         displayMatrix();
131     }else{//type of representation : list
132         cout<<"l"<<endl;
133         myFeed<<"l"<<endl;
134         displayList();
135     }
136 }
137 
138 void Graph::displayMatrix(){
139    /// int matrix[listVertex.size()][listVertex.size()];
140     ofstream myFeed("Files/Result.txt", ios::app);
141 
142     //calculation of the matrix
143 
144 
145     if(myFeed){
146         //display
147         for(int x=0; x<listVertex.size();x++){
148                 for(int y=0; y<listVertex.size();y++){
149                     myFeed<<matrix[x][y]<<";";
150                     cout<<matrix[x][y]<<";";
151                 }
152                 myFeed<<endl;
153                 cout<<endl;
154         }
155     }else{
156     cout<<"ERROR .TXT"<<endl;
157     }
158 }

代码有点长····主要就是数据结构中图的实现。主要看55-58行,上述程序运行后发生异常退出,如下图

 关闭窗口,build log出现Process terminated with status -1073741510

参考1:https://www.xuebuyuan.com/1149254.html

参考2:http://www.it1352.com/494518.html

最终发现是申请二维动态数组时的语法错误,55-58行应该改成:

matrix=new int*[nbNodes];
    for(int i=0;i<nbNodes;i++){
        matrix[i]=new int[nbNodes];
    }

好吧,好久不写代码很多基础知识都混乱了。

原文地址:https://www.cnblogs.com/mrlonely2018/p/11685814.html