分配两个二元数组
allocating two dimensinal array
>我有以下C++节点
unsigned int uiNoOfItems = 113;
unsigned int W = 2347;
// create two dimensional array
unsigned int ** optimalWeight = new unsigned int* [uiNoOfItems + 1];
for (unsigned int uiRowIdx = 0; uiRowIdx <= uiNoOfItems; uiRowIdx++) {
optimalWeight[uiRowIdx] = new unsigned int (W + 1);
}
std::cout << " initializing first column "<< std::endl;
// initialize first column
for (unsigned int uiRowIdx = 0; uiRowIdx <= uiNoOfItems; uiRowIdx++) {
optimalWeight[uiRowIdx][0] = 0;
}
std::cout << " initializing first row "<< std::endl;
// initialize first row
for (unsigned int uiColIdx = 0; uiColIdx <= W; uiColIdx++) {
// std::cout << uiColIdx << std::endl;
optimalWeight[0][uiColIdx] = 0; ------------------------> crash happens here at uiColIdx value 1210
}
上面的代码崩溃发生在提到的行。我不明白为什么,因为内存分配成功。
你也可以用这种方式做到这一点
unsigned const int uiNoOfItems = 113;
unsigned const int W = 2347;
int (*var)[W] = new int[uiNoOfItems][W];
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '