2012-09-29 123 views
0

,我有以下的代碼,我不能以下列方式更改釋放內存的指針結構

typedef struct 
{ 
    char* firstName; 
    char* lastName; 
    int id; 
    float mark; 
}* pStudentRecord; 

現在我分配內存:

g_ppRecords = (pStudentRecord*) malloc (sizeof(pStudentRecord*) *(g_numRecords));  /*allocate required memory for the array of pointers to instances of pStudentRecord*/ 

/*populate the data structure with records from the text file*/ 
while (count<g_numRecords) 
{ 
    fscanf(g_pf,"%s %s %i %f\n",&fn,&ln,&i,&m); 

    /*allocate memory for each student record. the pointer to each of these will be stored in the array g_ppRecords*/ 
    g_ppRecords[count]=(pStudentRecord)malloc(sizeof(char*)*2+sizeof(int)+sizeof(float)); 

    /*allocate memory for the firstName and lastName on the heap for each record. use only as much space as is required*/ 
    g_ppRecords[count]->firstName=(char*)malloc(strlen(fn)); 
    g_ppRecords[count]->lastName=(char*)malloc(strlen(ln)); 

    /*assign values stored in local variables to the ones on heap for each record*/ 
    strcpy(g_ppRecords[count]->firstName,fn); 
    strcpy(g_ppRecords[count]->lastName,ln); 
    g_ppRecords[count]->id=i; 
    g_ppRecords[count]->mark=m; 

    ++count; /*onto next record*/ 
} 

但是我有麻煩解放它。這是我在做什麼:

while (count<g_numRecords) 
{ 
     /*copy variables in the struct instance into local variables*/ 
    strcpy(fn,g_ppRecords[count]->firstName); 
    strcpy(ln,g_ppRecords[count]->lastName); 
    id=g_ppRecords[count]->id; 
    mark=g_ppRecords[count]->mark; 

    free(g_ppRecords[count]->firstName); 
    g_ppRecords[count]->firstName=NULL; 
    free(g_ppRecords[count]->lastName); 
    g_ppRecords[count]->lastName=NULL; 
    free(g_ppRecords[count]); 

    ++count; 

    #ifdef DEBUG 
     printf("\n%s %s %d %.2f",fn,ln,id,mark); 
    #endif 

    /*detect any errors while writing to file*/ 
    if(fprintf(g_pf,"%s %s %d %.2f\n",fn,ln,id,mark) ==-1) 
     perror(""); 
    else{ 
     #ifdef DEBUG 
      printf("success"); 
     #endif 
    } 

    #ifdef DEBUG 
     printf("return val=%d",now); 
    #endif 
} 

    free(g_ppRecords); /*free memory after writing to file is complete*/ 
    g_ppRecords=NULL; /*make pointer point to NULL*/ 
    fclose(g_pf);  /*close file stream*/ 
} 

我知道這可能與分配的內存爲:

g_ppRecords[count]=(pStudentRecord)malloc(sizeof(char*)*2+sizeof(int)+sizeof(float)); 

,但我不能把它分配爲

g_ppRecords[count]=(pStudentRecord)malloc(sizeof(pStudentRecord)); 

plz幫助!

+0

你確定'pStudentRecord'應該有最後一個'*'嗎? –

+0

你是說在這裏? g_ppRecords =(pStudentRecord *)malloc(sizeof(pStudentRecord *)*(g_numRecords)); –

+0

不,在你的struct typedef中。 –

回答

1

您的代碼應該是這樣的:

typedef struct 
{ 
    char* firstName; 
    char* lastName; 
    int id; 
    float mark; 
} StudentRecord; 

/* Allocate memory for each student record. */ 
g_ppRecords[count] = (StudentRecord*)malloc(sizeof(StudentRecord)); 

/* Allocate memory for the firstName and lastName on the heap. */ 
g_ppRecords[count]->firstName = (char*)malloc(strlen(fn)+1); 
g_ppRecords[count]->lastName = (char*)malloc(strlen(ln)+1); 

你是不是在琴絃培訓零分配空間,這會破壞內存。爲記錄本身分配空間時最好使用sizeof(struct)。此結構可能會在字段之間或整個記錄之後填充一些可能未包含在大小計算中的記錄。

如果你不能改變你的頭,你應該寫:

pStudentRecord DummyPtrVar; 
g_ppRecords = (pStudentRecord*)malloc(sizeof(*DummyPtrVar)*g_numRecords); 

寫作pStudentRecord*下的sizeof是不正確。這將創建雙指針而不是訪問記錄本身。 C/C++不允許簡單訪問指向類型。但是如果你定義了一個變量,你可以做一個僞解引用。這不是很整潔,但我相信可以和99%的編譯器一起工作。

+0

嘿基里爾,我想這些東西..但我的應用程序仍不能爲第一和最後的pStudentRecord的實例名稱可用內存。 我不能改變結構的聲明。它是我必須使用的代碼。 –

+0

我更新了我的答案。 –

0

我覺得你的代碼的主要問題是分配g_ppRecords的,

"g_ppRecords = (pStudentRecord*) malloc (sizeof(pStudentRecord*) *(g_numRecords));" 

應更改爲

"g_ppRecords = (pStudentRecord*) malloc (sizeof(pStudentRecord) *(g_numRecords));" 
當然

,作爲@Kirill說,字符數組應該留下更多的字節包含'\ n'。

+0

但我需要的數組是指針pStudentRecord而不是結構本身..你仍然說,我應該改變它的陣列? –