2012-07-28 2 views
0

힙 등에 관한 많은 질문이 있지만 매우 도움이되는 것은 아 닙니다 (왜 작동하지 않는지에 대한 이해 제외).힙 관련 질문

나는 엄청난 양의 데이터를 가지고 있으며 물론 힙을 추적 할 수 없다. 저장해야하는 데이터는 정수입니다. Malloc은 null을 아주 일찍 반환하기 시작합니다.

크기의

4 배열 : (malloc에 ​​의해 할당)

  • 875715
  • 875715
  • 875715
  • 5,105,043 세포 (그러나 그것은 2 차원 배열입니다) 여기

은 내 질문 :

1) quantit 메모리가 필요합니다. 875715 * 3 * 4 + 5105043 * 4 = 62454492? (정수 4이기 때문에) 약 62MB를 의미합니까? (멍청한 것 같아서 죄송합니다)

2) 사용 가능한 힙 크기를 어떻게 알 수 있습니까? 그것을 높이는 방법이 있습니까?

3) 같은 크기의 배열이 3 개 있습니다. 하나의 2D 배열에 병합하는 이점이 있습니까? 예를 들어 배열 [875715] [3]은 3 개의 다른 배열 대신에 (물론 malloc을 사용하여)

나는 창 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 : 저는 여기 1D 배열과 2 차원 배열 (첫 번째 수준)의 시작을 위해 할 일반적인 할당 내 프로그램은 실제로 작은 입력에 대한 매력처럼 작동합니다.

[..... 일부 코드는 .....] : 문제가 발생한 후입니다. 실패한 malloc은 getGraph 내부에 있으므로 나머지는 적절하지 않다고 생각합니다. 나중에 프로그램에서 배열을 free()합니다.

+0

1) int가 64 비트 시스템에서 32 비트인지 확인 하시겠습니까? 2) malloc을 '875715 * sizeof (int)'가 아닌 '875715' 값으로 호출합니까? –

+0

나는 포스트를 편집 해 봤어. – dyesdyes

+0

거기에 int 대신 sizeof 인수로 sizeGraph [0]을 사용하는 이유가 무엇입니까? 메모리 누수처럼 보입니다. –

답변

1
  1. 계산기를 사용하지 않고 코드를 분석하지 않아도 올바르게 분석됩니다.
  2. HEAP는 필요에 따라 확장되며 OS process limits.으로 제한됩니다. 기본적으로 윈도우에서는 더 이상 2Gig를 얻지 못합니다. 여기에 more specific link이 있습니다.
  3. 큰 장점은 없습니다.

귀하의 경우에는 더 이상 필요한 것을 할당하기 위해 메모리 할당 알고리즘을 조정하면 더 효과적입니다.

+0

호 똥! 당신 말이 맞아요. 파일을 수정 한 다음이 부분을 잊어 버린 부분을 다시 수정했습니다. 나는 너무 바보 같아. – dyesdyes