2017-06-05 45 views
-2

我在ubuntu 16.04 LTS中使用了帶有g ++ 5.4和CUDA 8.0的Eigen library version 3.3NVCC編譯特徵庫,並在運行時在結構中調整MatrixXd的大小失敗

編寫代碼時發生了令人困惑的事情。 當我嘗試在一個結構中調整Eigen :: MatrixXd時發生崩潰

結構如下。

struct cudaCopy{ 
     struct s_path_info *nodes_parents 
     struct s_path_info *nodes_children 
     ... 
} 

s_path_info結構如下。

struct s_path_info{ 
    Eigen::MatrixXd supps; 
    Eigen::MatrixXd residu; 
    ... 

問題如下。

struct cudaCopy *mem; 
mem = (struct cudaCopy*)malloc(sizeof(struct cudaCopy)); 

mem->nodes_parents = (struct s_path_info*)malloc(50 * sizeof(struct s_path_info)) 
for(int i=0; i<50; i++){ 
    mem->nodes_parents[i].supps.resize(1, 1); // ERROR 

下面是GDB執行代碼的回溯。

#0 __GI___libc_free (mem=0x1) at malloc.c:2949 
#1 0x000000000040a538 in Eigen::internal::aligned_free (ptr=0x1) at ../Eigen/Eigen/src/Core/util/Memory.h:177 
#2 0x000000000040f3e8 in Eigen::internal::conditional_aligned_free<true> (ptr=0x1) at ../Eigen/Eigen/src/Core/util/Memory.h:230 
#3 0x000000000040cdd4 in Eigen::internal::conditional_aligned_delete_auto<double, true> (ptr=0x1, size=7209728) at ../Eigen/Eigen/src/Core/util/Memory.h:416 
#4 0x000000000040d085 in Eigen::DenseStorage<double, -1, -1, -1, 0>::resize (this=0x6e1348, size=20, rows=20, cols=1) at ../Eigen/Eigen/src/Core/DenseStorage.h:406 
#5 0x000000000040ba9e in Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >::resize (this=0x6e1348, rows=20, cols=1) at ../Eigen/Eigen/src/Core/PlainObjectBase.h:293 
#6 0x000000000040697f in search::islsp_EstMMP_BF_reuse (this=0x7fffffffe4b0) at exam.cu:870 
#7 0x0000000000404f33 in main() at exam.cu:422 

有趣的是,下面的效果很好。

mem->nodes_children = (struct s_path_info*)malloc(10*50*sizeof(struct s_path_info)) 
for(int i=0; i<10*50; i++){ 
    mem->nodes_children[i].supps.resize(1, 1); // OK 

我不明白爲什麼nodes_parents無法調整大小。

我希望對此事有任何意見。謝謝。

+2

歡迎來到Stack Overflow。請花些時間閱讀[The Tour](http://stackoverflow.com/tour),並參閱[幫助中心](http://stackoverflow.com/help/asking)中的資料,瞭解您可以在這裏問。 –

回答

0

您將nodes_parents設置爲未初始化的內存,這意味着從未調用過s_path_info(以及其成員)的構造函數。 而不是struct s_path_info *nodes_parents你只要簡單地寫:

std::vector<s_path_info> nodes_parents; 

而主要cudaCopy應該只是一個堆棧變量:

cudaCopy mem; 

一般來說,不要使用malloc C++裏面,尤其不適合非吊艙。

N.B .:它與nodes_children一起使用的事實很可能是純粹的巧合。

相關問題