我知道论坛有很多关于堆等问题,但没有什么帮助我这么多(除了理解它为什么不起作用)。堆相关问题
我有大量的数据,当然堆不能跟随。我需要存储的数据只是整数。 Malloc很早就开始返回null。
4大小的阵列:(由malloc分配)
- 5105043细胞(但它是一个二维阵列)
这里是我问题:
1)知道定量y是否需要内存: 875715 * 3 * 4 + 5105043 * 4 = 62454492? (因为整数是4) 这是否意味着大约62 MB? (对不起,如果它看起来愚蠢)
2)我们如何知道堆的大小可用?有没有办法增加它?
3)我有3个相同大小的数组,是否有合并到一个2D数组的优势? 例如,数组[875715] [3]而不是3个不同的数组(当然,使用malloc)
我使用Window 7,64位,8GB的RAM。
编辑:这是一个典型的分配我的一维数组和二维数组(第一级)的开始做:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define FILE_NAME "SCC.txt"
#define ARRAY_SIZE 875714
void getGgraph(int mode,int **graph,int *sizeGraph);
int sizeOfArray(int *array);
void getGraphSize(int mode,int *arr);
void runThroughGraph(int node,int **graph,int *exploredNode,int *magicalPath,int *sizeGraph);
void getMagicalPath(int *magicalPath,int **graph,int *sizeGraph);
void main()
{
int i, *magicalPath,*sizeGraph, **graph;
/* ------------- creation of the array sizeGraph ------------------ */ // contain the size of each level to initiate the array
if ((sizeGraph =(int*) malloc((ARRAY_SIZE + 1) * sizeof(sizeGraph[0]))) == NULL) {
printf("malloc of sizeGraph error\n");
return;
}
memset(sizeGraph, 0, (ARRAY_SIZE + 1) * sizeof(sizeGraph[0]));
/* ------------- create reverse G graph, this will be a 2D array ------------------ */
if ((graph =(int**) malloc((ARRAY_SIZE + 1) * sizeof(*graph))) == NULL) {
printf("malloc of graph error\n");
return;
}
getGgraph(1,graph,sizeGraph);
// [..... Some more code .....]
// end of main()
}
void getGgraph(int mode,int **graph,int *sizeGraph) {
char int_string[40];
char stringToAdd[10];
FILE *integerFile = NULL;
int i = 0, j = 0, n = 0,stCurrentInt, tail,head,*temp;
getGraphSize(mode,sizeGraph);
for (i = 0; i < (ARRAY_SIZE + 1); i++) {
if ((graph[i] =(int*) malloc((ARRAY_SIZE + 1) * sizeof(graph[i][0]))) == NULL) {
// THIS IS WHERE IT STOPS (i = 594)
printf("Malloc of graph[%d] error\n",i);
return;
}
}
if ((temp =(int*) malloc((ARRAY_SIZE + 1) * sizeof(temp[0]))) == NULL) {
printf("malloc of temp in getGgraph function error\n");
return;
}
memset(temp, 0, (ARRAY_SIZE + 1) * sizeof(temp[0]));
if ((integerFile = fopen(FILE_NAME, "r")) != NULL) {
while (fgets(int_string,40, integerFile) != NULL) {
n = 0, i = 0, stCurrentInt = 0,head = 0; // initialisation
while (int_string[n] != NULL) {
if (int_string[n] == ' ') {
for (j = stCurrentInt; j < n; j++) {
stringToAdd[j - stCurrentInt] = int_string[j];
}
if (stCurrentInt == 0) // first integer is the index
tail = (int) atoi(stringToAdd);
else {
head = atoi(stringToAdd);
if (mode == 0) {
graph[tail][temp[tail]] = head;
temp[tail]++;
}
else if (mode == 1) {
graph[head][temp[head]] = tail;
temp[head]++;
}
}
for (j = 0; j < 10; j++) { // empty the string for next iteration
stringToAdd[j] = NULL;
}
stCurrentInt = n + 1;
}
n++;
}
}
free(temp);
fclose(integerFile);
}
else {
printf("\n File missing in getGgraph.\n");
return;
}
}
void getGraphSize(int mode,int *arr) {
char int_string[40],stringToAdd[10];
FILE *integerFile = NULL;
int i = 0, j = 0, n = 0,stCurrentInt,tail,head;
if ((integerFile = fopen(FILE_NAME, "r")) != NULL) {
while (fgets(int_string,40, integerFile) != NULL) {
n = 0, i = 0, stCurrentInt = 0,head = 0; // initialisation
while (int_string[n] != NULL) {
if (int_string[n] == ' ') {
for (j = stCurrentInt; j < n; j++) {
stringToAdd[j - stCurrentInt] = int_string[j];
}
if (stCurrentInt == 0) // first integer is the index
tail = (int) atoi(stringToAdd);
else
head = atoi(stringToAdd);
for (j = 0; j < 10; j++) { // empty the string for next iteration
stringToAdd[j] = NULL;
}
stCurrentInt = n + 1;
}
n++;
}
if (mode == 0 && head != 0)
arr[tail]++;
else if (mode == 1 && head != 0)
arr[head]++;
}
}
else {
printf("\n File missing in getGraphSize.\n");
return;
}
}
EDIT2:我的节目其实就像较小的输入的魅力。
[.....更多代码.....]:这是问题之后。失败的malloc位于getGraph内部,所以我认为其余部分不相关。我稍后在程序中释放()数组。
1)你确定你的64位系统的整数是32位吗? 2)你可以用875715这个值调用malloc,而不是875715 * sizeof(int)'? – 2012-07-28 11:15:18
我已编辑帖子,看看。 – dyesdyes 2012-07-28 11:18:23
是否有一个原因为什么你使用sizeGraph [0]作为sizeof而不是int的参数?看起来像内存泄漏。 – 2012-07-28 11:20:47