2010-12-09 26 views
1
int main(void) 
{ 
    int byte; 
    int i= 0,zj,di; 
    int n = 0; 
    char y[100]; 
    char e[100]; 
    char f1[100]; 
    int **arr; 
    int *arr1; 

    if(argc < 2) 
    { 
     printf("Input file does not exist:\n"); 
     printf("Enter arguments in the following manner:\n"); 
     printf("<./a.out> <input_file> <output_file>:\n"); 
     return 0; 
    } 

    fp1=fopen(argv[1],"r"); 

    if (fp1 == NULL){ 
     printf("input file does not exist.\n"); 
     return EOF; 
    } 

    if(argc < 3) 
    { 
     fp=stdout; 
    } 
    else 
     fp=fopen(argv[2],"w"); 

    for(i=0;i<4;i++){ 
     fscanf(fp1,"%s",y); 
     a = atoi(y); 
     if(i==0)  zj = a; 
     if(i==1)  di = a; 
     if(i==2)  ck = a; 
     if(i==3)  n = a; 
    } 

    arr =(int**) memory_Malloc(n*sizeof(int*)); 
    for (i=0; i<n; ++i){ 
     arr[i] = memory_Malloc(2*sizeof(int)); 
    } 

    arr1=(int*)memory_Malloc(di*sizeof(int)); 
    for (i=0;i<zj;i++){ 
     arr1[i]=0; 
    } 

    fgets(y,100,fp1); 
    count=0; 
    while (!feof(fp1)){ 
     fgets(y, 100, fp1); 
     if(strlen(y)<=1) 
      break; 
     if(count>=zj) 
      break; 
     sscanf(y,"%s %s", e, f1); 
     arr[count][0] = atoi(e); 
     arr[count][1] = atoi(f1); 
     count++; 
    } 

    for(i=0; i<n ; i++) 
    { 
     memory_Free(arr[i],2*sizeof(int)); 
    } 
    memory_Free(arr,n*sizeof(int)); 
    memory_Free(arr1,di*sizeof(int)); 

    fclose(fp1); 

    // intermediate code and then // 

    fclose(fp); 
} 

[編輯]無法在下面的代碼找到一個字節的內存泄漏

1,600,000 bytes in 1 blocks are definitely lost in loss record 2 of 2 
    ==15369== at 0x43DC38B: malloc (vg_replace_malloc.c:149) 
    ==15369== by 0x8056080: memory_Malloc (memory.c:567) 
    ==15369== by 0x804D51B: main (symbol.h:649) 
    ==15369== 
    ==15369== LEAK SUMMARY: 
    ==15369== definitely lost: 1,600,000 bytes in 1 blocks. 
    ==15369==  possibly lost: 0 bytes in 0 blocks. 
    ==15369== still reachable: 96 bytes in 12 blocks. 
    ==15369==   suppressed: 0 bytes in 0 blocks. 

[編輯]

POINTER memory_Malloc(unsigned int Bytes) // same as malloc in standard C 
    void memory_Free(POINTER Freepointer, unsigned int Size) 
    int * PRECEDENCE 
    PRECEDENCE P 
    symbol.h 649 P = memory_Malloc(sizeof(int[symbol__MAXSIGNATURE])); 
+3

我把你的發佈代碼儘可能地清理乾淨 - 這真是一團糟 - 如果你想讓人們花時間閱讀你的代碼並幫助你,請在代碼格式化方面加倍努力。 – 2010-12-09 11:26:16

回答

2
arr1=(int*)memory_Malloc(di*sizeof(int)); 
    for (i=0;i<zj;i++){ 
     arr1[i]=0; 
    } 
  • 什麼是memory_Malloc
  • 如果zjdi時,你將在arr1

    fgets(y,100,fp1); 
    count=0; 
    while (!feof(fp1)){ 
        fgets(y, 100, fp1); 
    
  • 外面寫拿到1 yfp1,然後開始循環,然後得到另一yfp1:你剛剛「丟失「第一個y

    if(count>=zj) 
    
  • S這不是count>=n?數組arr有空間用於n元素。

    memory_Free(arr,n*sizeof(int)); 
    
  • 什麼是memory_Free()

  • 你爲什麼要傳遞2個參數?
  • arr大小不是n*sizeof(int):如您在memory_Malloc寫道,這是n*sizeof(int*)
2

我想我已經得到了它。

arr大小不是n*sizeof(int) =他們是指針,所以應該是n*sizeof(int*)而不是。