分配两个二元数组

allocating two dimensinal array

本文关键字:二元 数组 两个 分配      更新时间:2023-10-16

>我有以下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];