CudaMalloc抛出sigabrt错误
CudaMalloc throws sigabrt error
我被这个问题卡住了。问题是在执行cudaMalloc
时出现分段故障。这就是我正在做的:
class AllInput {
public:
int numProducts;
Product * products;
public:
AllInput(int _numProducts, Product * _products);
};
class Product {
public:
int sellingPrice; //Ri
struct DemandDistribution observationDemand; //C2i
public:
Product(
LucyDecimal _sellingPrice, //Ri
LucyDecimal _costPriceAssmbly);
};
然后我有一个创建它的函数:
AllInput* in1() {
struct DemandDistribution * _observationDemand1 =
(DemandDistribution*) malloc(sizeof(DemandDistribution));
// set values
Product * product1 = new Product(165,_observationDemand1);
//initialize product2, product3, product4
Product *products = (Product*) malloc(4 * sizeof(Product*)); //line-a
products[0] = * product1;
products[1] = * product2;
products[2] = * product3;
products[3] = * product4;
AllInput* all = new AllInput(4, products);
return all;
}
当我尝试这样做时:
void mainRun(){
AllInput* in = in1();
AllInput* deviceIn;
deviceIn = new AllInput(0, NULL);
cudaMalloc((void**) &deviceIn, sizeof(AllInput*)); //line-b
line-b
抛出分段错误。如果我将line-a
更改为Product products[4] = { *product1, * product2, *product3, *product4};
,则错误消失。这不是解决方案,因为products
变成了解构的
更改products
对cudaMalloc
有何影响?我们没有向cudaMalloc
传递任何论点,但它为什么会影响它?我该怎么做才能避免这种情况?
问题可能是线路
(Product*) malloc(4 * sizeof(Product*));
创建一个由四个指针组成的数组。如果Product
大于指针(在您的示例中可能是这样),那么接下来的4行就是缓冲区溢出。堆可能已损坏,malloc内部数据也被覆盖——此外,您还可以覆盖堆的一些随机部分。
该行应该是(Product*) malloc(4 * sizeof(Product))
、(Product *)malloc(sizeof(Product[4]))
或更好的new Product[4]
(注意,在最后一种情况下,您应该在delete[]
之前释放)。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 为什么我在此代码中收到 SIGABRT 错误
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- 当我的代码工作正常时,为什么在SPOJ中遇到运行时(Sigabrt)错误
- CPP中的Sigabrt错误
- 在数组中移动元素 SIGABRT 错误
- 由于 getline(cin,s) 导致的 SIGABRT 错误
- 在Hepsort程序中获取Sigabrt错误
- SIGABRT 运行时何时出现错误以及如何在这种情况下进行调试
- 链接列表的SIGABRT错误
- 矢量和双克初始化或push_back会导致 SIGABRT 错误
- C++中的SIGABRT错误
- 用GDB中更有意义的错误描述替换SIGABRT
- CudaMalloc抛出sigabrt错误