我不知道是什么导致了这个问题。我在最后一行得到"访问冲突写入位置"错误。我没有正确分配内存吗?
typedef struct {
doubleXYZW cen_sum; //struct with 4 doubles
double STS[6];
XYZW *Points;// //struct with 4 floats
}BUNDLE;
BUNDLE *cpu_data = NULL;
size_t bundle_size = NUM_POINTS * sizeof(XYZW) + sizeof(doubleXYZW) + 6*sizeof(double);
HANDLE_ERROR(cudaMallocHost((BUNDLE**)&cpu_data, bundle_size));
//error in the next line
cpu_data->Points[0].x = 0; //x is the first element in the XYZW struct
您有两个必须完成的分配,而您只执行其中一个。
您正在为cpu_data
指针分配一些存储空间,但是您没有为Points
指针分配任何存储空间。因此当你解参点时:
cpu_data->Points[0].x = 0;
^ ^
| this dereferences the Points pointer (NOT allocated!)
|
this dereferences the cpu_data pointer (allocated)
是对一个没有分配的指针解引用,所以它是无效的。尝试以这种方式访问某些内容将生成无效访问。
你有(至少)两个选项来修复它:
- 在为
cpu_points
分配空间之后,您可以在cpu_points->Points
上执行另一个cudaMallocHost
分配 如果你知道
Points
数组的大小(似乎你做-NUM_POINTS
),那么你可以静态地为它分配:typedef struct { doubleXYZW cen_sum; //struct with 4 doubles double STS[6]; XYZW Points[NUM_POINTS];// //struct with 4 floats }BUNDLE;
注意,您的bundle_size
计算是按照建议使用第二种方法的方式进行的。如果您使用第一种方法,您的bundle_size
计算不正确。在任何情况下,无论使用哪一种方法,都更容易将bundle_size
计算为sizeof(BUNDLE)
。
要清楚,这里没有特定于cuda的东西(例如,如果您使用malloc
而不是cudaMallocHost
,则会出现错误)。