【问题标题】:Freeing memory for a pointer to struct为指向结构的指针释放内存
【发布时间】:2012-09-29 03:43:12
【问题描述】:

我有以下无法更改的代码

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));

请帮忙!

【问题讨论】:

  • 你确定pStudentRecord 应该有最后一个* 吗?
  • 你的意思是在这里? g_ppRecords=(pStudentRecord*) malloc (sizeof(pStudentRecord*) *(g_numRecords));
  • 是的,这是由教授提供的,它必须在那里!我不能使用 malloc (sizeof (pStudentRecord) ) 因为...
  • "但我无法将其分配为 .." 实际问题是什么?有什么错误信息吗?
  • 哇!我实际上能够做 g_ppRecords[count]=(pStudentRecord)malloc(sizeof(pStudentRecord));但我仍然遇到运行时崩溃。真的不知道发生了什么尝试分配 g_ppRecords[count]->firstName=(char*)malloc(strlen(fn)+1);而不是 g_ppRecords[count]->firstName=(char*)malloc(strlen(fn)); (因为我后来做了一个 strcpy 并且会添加一个 \0 char .. 但这也没有帮助

标签: c pointers struct free


【解决方案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% 的编译器都可以使用。

【讨论】:

  • 嘿基里尔,我尝试了这些东西..但我的应用程序仍然无法为 pStudentRecord 实例释放名字和姓氏的内存。我不允许更改结构的声明。它是我必须按原样使用的代码。
【解决方案2】:

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

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

应该改为

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

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

【讨论】:

  • 但是我需要数组是指向 pStudentRecord 的指针数组,而不是结构本身.. 你还是说我应该改变它?
猜你喜欢
  • 1970-01-01
  • 2017-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
相关资源
最近更新 更多