释放内存以获取指向struct的指针

时间:2012-09-29 03:43:12

标签: c pointers struct free

我有以下代码,我无法更改

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帮助!

2 个答案:

答案 0 :(得分: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);   

在sizeof下写pStudentRecord*是不正确的。这将创建双指针而不是访问记录本身。 C / C ++不允许简单访问指向类型。但是,如果定义变量,则可以进行伪解除引用。这不是很整洁但我肯定会与99%的编译器一起使用。

答案 1 :(得分:0)

我认为你的代码的主要问题是g_ppRecords的分配,

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

应该改为

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

当然,正如@Kirill所说,char数组应该留下一个更多的字节来包含'\ n'。