我試圖編譯使用GCC一個64位CPP代碼,然而多維(即2D)陣列的存儲器分配返回NULL
一次我從46000增大元件尺寸至46,500。我的虛擬內存設置爲96GB,並且硬件正在運行使用32GB Ram的64位操作系統。 只要MAX_VERTICES
不超過46000
代碼工作正常。2D陣列內存分配(malloc的)返回NULL
以下是我試圖動態分配:
struct ShortestPath {
real32 totalWeight;
// NOTE: ShortestPath is a list of pointers; does not make copies
// (i.e. side-effects) the pointers point to memory allocated
// in the DijkstraSPTree array in the vehicle_searching module
List<DirectedEdge *> *edgeList;
};
#define MAX_VERTICES 46500
global_variable ShortestPath spAllPairs[MAX_VERTICES][MAX_VERTICES];
在堆上分配內存用下面的代碼替換
spAllPairs[MAX_VERTICES][MAX_VERTICES]
global_variable ShortestPath **spAllPairs;
global_variable ShortestPath *arr_data;
ShortestPath *getShortestPath(EdgeWeightedDigraph *digraph, int32 source,
int32 dest)
{
free(spAllPairs); // Function is called multiple times so I clear memory
free(arr_data); // before reallocation given values pointed by pointers
free(spTreesArray); // are used in other files in my project after run.
inline allocate_mem(ShortestPath*** arr, ShortestPath** arr_data, int n, int m);
allocate_mem(&spAllPairs, &arr_data, MAX_VERTICES, MAX_VERTICES);
for (unsigned int k = 0 ; k < MAX_VERTICES ; k++) {
if (spAllPairs[k] == NULL) {
while (k >= 1) {
free(spAllPairs[k]);
--k;
}
free(spAllPairs[0]);
free(spAllPairs);
fprintf(stderr, "Failed to allocate space for Shortest Path Pairs!\n");
exit(1);
}
}
spTreesArray = (DijkstraSPTree *)malloc(MAX_VERTICES * sizeof(DijkstraSPTree));
for (int32 vertexTo = 0; vertexTo < digraph->vertices; ++vertexTo) {
pathTo(&spTreesArray[source], &spAllPairs[source][vertexTo],
vertexTo);
}
return &spAllPairs[source][dest];
}
void pathTo(DijkstraSPTree *spTree, ShortestPath *shortestPath, int32 dest)
{
List<DirectedEdge *>::traverseList(freeDirectedEdge, shortestPath->edgeList);
List<DirectedEdge *>::emptyList(&shortestPath->edgeList);
shortestPath->totalWeight = spTree->distTo[dest];
}
int allocate_mem(ShortestPath ***arr, ShortestPath **arr_data, int n, int m)
{
*arr = (ShortestPath **)malloc(n * sizeof(ShortestPath*));
*arr_data = (ShortestPath *)malloc(n * m * sizeof(ShortestPath));
for (int i = 0; i < n; i++)
(*arr)[i] = *arr_data + i * m;
return 0; //free point
}
請記住,在堆分配必須是連續的* *。你試圖分配32個內存(如果'sizeof(ShortestPath)== 16'最有可能)作爲一個大塊。如果沒有這麼大的連續內存塊,分配將失敗。 –
'46000 x 46000'低於1.97 Gigs,而'46500 x 46500'是2.013 Gig。如果你的結構體的大小是16,那麼'46000'可能會保持在32GB以下,而46500的產量超過32GB。檢查你的'mallocs'的結果,他們應該得到NULL在某些時候 –
@StephanLechner物理內存應該不重要,因爲我使用的是malloc,而虛擬內存是96GB。我不確定它是否是連續的。在「spAllPairs」的初始化期間,我在46500x46500處獲得NULL。 – Far