2017-03-28 18 views
0

私はVehicle Routing Problem(VRP)を解決しようとしていますが、呼び出し元の関数に値が返されない限り動作しているようです。私はベクトル、配列、配列、ベクトルのポインタを返そうとしましたが、何も動作していないようです。実際には、以前に宣言され、正しく機能しているベクターのいくつかはうまくいきません。私はWindowsの64ビットプラットフォーム上でGCCを使用していますし、エラーがvector<int> tabout = Ridesharing::CVRP(Vertex, X, Y, Demand);ランタイムエラー:参照のベクトルを返します

で実行時に表示されます__gnu_cxx :: new_allocator :: DEALLOCATE(これ= 0x22db20、__P = 0x9a90e0)(C:/プログラムファイル/ mingwの-W64/x86_64-5.3.0-のWin32-SEH-rt_v4-rev0の/にMinGW64/x86_64の-W64-MINGW32 /含む/ C++/EXT/new_allocator.h:110)の変数の

~vector() _GLIBCXX_NOEXCEPT 
    { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, 
      _M_get_Tp_allocator()); } 

// LEADING UP TO... 
// __p is not permitted to be a null pointer. 
    void 
    deallocate(pointer __p, size_type) 
    { ::operator delete(__p); } 

フォーマル宣言

struct Vertex{ 
    int id; 
    double x; 
    double y; 
    double demand; 
}; 
vector<int> *tabout;      //Output 
int vertex_num;       
int capacity;           
int verify = 0;       
vector<Vertex> vertex;       
vector<vector<int> > population; 
//int** population;      //Set of solutions in the population 
double cur_best_distance;    
vector<int> cur_best_individual;   
double best_solution; 

vector<int> 
Ride::CVRP(vector<int> id, vector<double> x, vector<double> y, vector<double> demand) 
{ 
    vertex_num = id.size()-1; 
    capacity = 8; 
    best_solution = 99999999; 
    population.resize(pop_size, vector<int>(vertex_num)); 
    best_individual = new int[vertex_num]; 
    vertex.resize(vertex_num); 

    for (unsigned i = 0; i <= vertex_num; i++){ 
     vertex[i].id = id[i]; 
     vertex[i].x = x[i]; 
     vertex[i].y = y[i]; 
     vertex[i].demand = demand[i]; 
    } 

    initialize_population(); 

    int generation = 1; 

    while (generation < max_generation){ 

     choose_individuals(best_individual); 

     individuals_transform(); 

     //cout <<"the best in current generation: "<<cur_best_distance<< endl; 
     generation ++; 
    } 
// COUT RESULTS IN RUNTIME ERROR AT THIS LINE! 
    printf("The cost of the best solution is: %0.3f", best_solution);  
if (verify == 1) 
     cout << "\nThe optimal route is visited in the following order: "; 


     vector <int> result = final_solution(best_individual); 

     tabout = new vector<int>(result.size()); 
     vector<int> &temp = *tabout; //Create a reference 
     for (int i = 0; i < result.size(); i++){ 
      temp[i] = result[i]; // SET OF VERTEX VISITED AS OUTPUT 
    cout << temp[i]<<" "; 
} 
    cout << endl; 
if(verify == 0) 
    cout<<"\nNo feasible solution found"<<endl; 

delete[] best_individual; 
best_individual = NULL; 
// system("pause"); 
    return temp; // VECTOR SUCCESSFULLY RETURNED AS REFERENCE 
} 

呼び出し関数:

void main(); 
{ 

vector<int> tabout = Ridesharing::CVRP(Vertex, X, Y, Demand); // RUNTIME ERROR AT THIS LINE, HOWEVER DEBUG SHOWS `tabout` with assigned values from `temp` 
      std::vector<std::vector<int>> taborder(0, std::vector<int>(tabout.size())); // Defaults to zero initial value 
      for(unsigned i =0; i < tabout.size(); i++){ 
       cout<<tabout[i]<<" "; 
       taborder[0][i] = tabout[i]; 
      } 
      delete &tabout; //Delete the reference 
      cout<<endl; 
      for (unsigned s = 0; s < taborder.size(); s++) 
       for (unsigned m = 0; m < taborder[s].size(); m++) 
        cout<<Vertex[taborder[s][m]]<< " "; 
      cout<<endl; 
} 

この実行時エラーを回避するには、いくつかの実装方法を試してみましたが、何も問題はありませんでした。 coutはなぜ機能しませんか?この未定義の動作の原因を特定することはできませんが、すべてのエラーはstl_vector.hに関連しているようです。

+0

'ベクトル tabout'は自動ストレージにあるので、' delete&tabout; 'は非常に悪い考えです。 – user4581301

+0

@ user4581301私は 'tabout = new vector (result.size());を使っていましたが、とにかくポインタの代わりにオブジェクトのベクトルを使うためにこれをすべて削除しました。 @ 1201ProgramAlarmの提案と小さな調整を組み合わせることでこの問題が解決されました。 – Far

+0

それはホールドオーバーであると考えられていました。コードの他の部分との関連で十分な意味を持たなかった。 – user4581301

答えて

0

割り当てられた配列の境界を超えて書いています。 vertex_num要素の領域をに割り当てます。番号は0vertex_num-1です。初期化ループ(for (unsigned i = 0; i <= vertex_num; i++))はvertex[vertex_num]にアクセスし、配列の末尾を越えて書き込みを行うと、未定義の動作となり、他のメモリが破損します。

vertexにもう1つの要素を割り当てるか、ループ内でi < vertex_numを使用する必要があります。

+0

'vertex.resize(vertex_num + 1);'が変更されましたが、何も変更されませんでした。 – Far

+0

修正しました。 'std :: vector > taborder(0、std :: vector (tabout.size()));サイズが1より大きい2次元ベクトルではなく、サイズ0を初期化します。また、' vertex .resize(vertex_num) 'を' vertex.resize(vertex_num + 1) 'に設定します。 – Far

+0

STLコンテナに関連付けられているエラーをさらに検出するには、_GLIBCXX_DEBUGを強くお勧めします。 – Far

関連する問題