Я пытаюсь скомпилировать 64-битного кода CPP с использованием GCC, однако многомерное (т.е. 2D) распределение памяти массив возвращает NULL
раз увеличить размер элемента из до . Моя виртуальная память установлена на 96 ГБ, а на оборудовании работает 64-разрядная ОС с 32 ГБ памяти. Код работает нормально, покаMAX_VERTICES
не превышает46000
.2D Выделение массива памяти (таНос) Возвращает 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];
Выделяют память на Heap заменить
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 концерта, тогда как' 46500 x 46500' составляет 2,013 концерта. Если размер вашей структуры равен 16, то «46000» может оставаться ниже 32 ГБ, тогда как 46500 дает более 32 ГБ. Проверьте результаты вашего 'mallocs', они должны получить NULL в какой-то момент –
@StephanLechner Физическая память не имеет значения, так как я использую malloc, а виртуальная память - 96 ГБ. Я не уверен, что это смежно. Я получаю NULL при 46500x46500 во время инициализации «spAllPairs». – Far