2016-02-05 97 views
1

我的代碼有內存泄漏問題。我不知道我錯在哪裏。下面是代碼:我想從csv文件讀取並存儲特定的列。從csv文件和動態內存分配中讀取

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
int main() 
{ 
    FILE *result = fopen ("C:\\Users\\pa1rs\\Desktop\\local.csv", "w"); 

    const char *text = "LOA,NAME,"; 
    fprintf (result, "%s", text); 
    char *token; 
    char *endToken; 
    int lines = 0; 
    char ch;       /* should check the result */ 
    FILE *file = fopen ("C:\\Users\\pa1rs\\Desktop\\samplee.csv", "r"); 
    char line[300]; 
    if (file == NULL) { 
     perror ("Error opening the file"); 
    } else { 
     while (!feof (file)) { 
      ch = fgetc (file); 
      if (ch == '\n') { 
       lines = lines + 1; 
      } 
     } 
     //printf(" no of lines existing in the file %d\n\n", lines); 
    } 
    fseek (file, 0, SEEK_SET); 
    while ((ch = fgetc (file)) != '\n') { 
     // we don't need the first line on sample.csv 
     // as it is just the description part 
    } 
    int s[lines - 1]; 
    int j = 0; 
    char *N[lines - 1]; 
    while (fgets (line, sizeof (line), file)) { 
     int i = 0; 
     token = line; 
     do { 
      endToken = strchr (token, ','); 
      if (endToken) 
       *endToken = '\0'; 
      if (i == 3) { 
       s[j] = atoi (token); 
      } 
      if (i == 12) { 
       N[j] = (char *) malloc (strlen (token) * sizeof (char)); 
       strcpy (N[j], token); 
      } 

      if (endToken) 
       token = endToken + 1; 
      i++; 
     } while (endToken); 
     j = j + 1; 
    } 
//******************************************************unigue loa 
    int count = 0; 
    int g = 0; 
    int h = 0; 
    int LOA[lines - 1]; 
    int dd = 0; 
    for (dd = 0; dd < lines - 1; dd++) { 
     LOA[dd] = 0; 
    } 
    for (g = 0; g < lines - 1; g++) { 
     for (h = 0; h < count; h++) { 
      if (s[g] == LOA[h]) 
       break; 
     } 
     if (h == count) { 
      LOA[count] = s[g]; 
      count++; 
     } 
    } 
    int xw = 0; 
    for (xw = 0; xw < count; xw++) { 
     //printf("%d \t",LOA[xw]); 
    } 

    //printf("LOA Array Length is: %d \n",count); 


    //******************************************************** 
    ////FOR UNIQUE NAMES ARRAY 


    //printf("No of unique names are %d",county); 
    //FOR UNIQUE CAUSES ARRAY 
    char *sa[9] = 
     { "Monticello", "Valparaiso", "Crown Point", "Plymouth", "Goshen", 
"Gary", "Hammond", "Laporte", "Angola" }; 
    int countz = 0; 
    int gz = 0; 
    int hz = 0; 
    char *LOAz[lines - 1]; 
    int zero2 = 0; 
    for (zero2 = 0; zero2 < lines - 1; zero2++) { 
     LOAz[zero2] = NULL; 
    } 
    for (gz = 0; gz < lines - 1; gz++) { 
     for (hz = 0; hz < countz; hz++) { 
      if (strcmp (N[gz], LOAz[hz]) == 0) 
       break; 
     } 
     if (hz == countz) { 
      LOAz[countz] = (char *) malloc (strlen (N[gz]) * sizeof (char)); 
      strcpy (LOAz[countz], N[gz]); 
      countz++; 
     } 
    } 
    int nz = 0; 
    for (nz = 0; nz < countz; nz++) { 
     fprintf (result, "%s,", LOAz[nz]); 
    } 
    fprintf (result, "\n"); 
    // printf("%d",countz); 
    //***************************** 
    int i = 0; 
    int jjj = 0; 
    int xxx = 0; 
    int ggg = 0; 
    int k = 0; 
    int kount[count][countz]; 
    for (xxx = 0; xxx < count; xxx++) { 
     for (ggg = 0; ggg < countz; ggg++) { 
      kount[xxx][ggg] = 0; 
     } 
    } 
    for (i = 0; i < count; i++) { 
     for (k = 0; k < countz; k++) { 
      for (jjj = 0; jjj < lines - 1; jjj++) { 
       if (LOA[i] == s[jjj]) { 
        if (strcmp (LOAz[k], N[jjj]) == 0) { 
         kount[i][k]++; 

        } 
       } 

      } 
     } 
    } 

    int ig = 0; 
    int ik = 0; 
    for (ig = 0; ig < count; ig++) { 
     fprintf (result, "%d,%s", LOA[ig], sa[ig]); 
     for (ik = 0; ik < countz; ik++) { 
      fprintf (result, ",%d", kount[ig][ik]); 
     } 
     fprintf (result, "\n"); 
    } 
    int rrr = 0; 
    free (N); 
    for (rrr = 0; rrr < lines - 1; rrr++) { 
     free (LOAz[rrr]); 
    } 

    //***************************** 
    //fclose(result); 
    fclose (file); 
    return 0; 
} 

行我來到這裏是13761和LOAz與陣列尺寸線-1 = 13761宣佈,但唯一的人我來到這裏只有49歲,所以我重新分配了內存和剩餘未使用,我覺得問題從那裏開始。 請幫忙!提前致謝。

+0

要跟蹤內存泄漏,您需要使用像Valgrind(https://valgrind.org)這樣的內存檢查程序。 – alk

+0

用'FILE * result = fopen'打開後,您無法關閉'result'。假設'rrr'在'LOAz [countz]'中循環遍歷'countz',那麼關閉文件失敗是唯一的泄漏。另外,請組織你的聲明。您通常應該在每個代碼塊的開頭聲明您的變量,而不是在每個塊中的臨時噴灑中。 –

回答

1

您代碼中的一個問題是您沒有爲字符串分配足夠的內存。例如,在這些行:

N[j] = (char*) malloc(strlen(token) * sizeof(char)); 
strcpy(N[j], token); 
// ... 
LOAz[countz] = (char*) malloc(strlen(N[gz]) * sizeof(char)); 
strcpy(LOAz[countz], N[gz]); 

的問題是,strlen返回字符串中的非零符號的數目。但是,要存儲字符串,您還需要多一個字節,以存儲零終止字符,因此存儲s的緩衝區大小應至少爲strlen(s) + 1

此外,更好的編碼風格是to avoid casting the return value of malloc

+2

sizeof(char)也相當無用 –

+0

...演員也是如此。 – alk