在C语言中释放结构元素

在C语言中释放结构元素,c,C,我有一个结构: struct student{ int roll_no; char *name = malloc(25 * sizeof(char));; char *phone_no = malloc(10 * sizeof(char));; char *dob = malloc(10 * sizeof(char));; }*s1; int main(){ s1 = malloc(5 * sizeof(student)); //array of stu

我有一个结构:

struct student{
    int roll_no;
    char *name = malloc(25 * sizeof(char));;
    char *phone_no = malloc(10 * sizeof(char));;
    char *dob = malloc(10 * sizeof(char));;
}*s1;

int main(){
    s1 = malloc(5 * sizeof(student)); //array of student
    //.....
}
对于分配大小为“n”的学生数组并随后取消分配的完整循环,什么是合适的代码? 注意:这里的问题涉及结构实例元素的分配和取消分配。

typedef struct student{
    int roll_no;    // (the following illegal syntax commented out)
    char *name;     // = malloc(25 * sizeof(char));;
    char *phone_no; // = malloc(10 * sizeof(char));;
    char *dob;      // = malloc(10 * sizeof(char));;
}*s1;
…根据所描述的需求,(减去非法转让声明),可能更好的形式是:

typedef struct {
    int roll_no;
    char *name;    //needs memory
    char *phone;   //needs memory
    char *dob;     //needs memory
}STUDENT;          
然后,使用新的变量类型:
STUDENT
,根据需要创建结构实例。您的OP表明您需要5:

STUDENT s[5];     //Although this array needs no memory, the 
                  //members referenced by it do 
                  //(as indicated above)
现在,需要做的就是在5个实例中的每个实例中为需要它的3个成员创建内存

for(i=0;i<5;i++)
{
    s[i].name  = calloc(80, 1); //calloc creates AND initializes memory.
    s[i].phone = calloc(20, 1); //therefore safer than malloc IMO.
    s[i].dob   = calloc(20, 1); //Also, change values as needed to support actual
                                //length needs for name, phone and dob
}
// Use the string members of s[i] as you would any other string, But do not
// forget to free them when no longer needed.
...
for(i=0;i<5;i++)
{
    free(s[i].name);
    free(s[i].phone);
    free(s[i].dob);
}

除了Ryker的答案之外,如果您想动态执行此操作:

#include <stdlib.h>

struct student{
    int roll_no;
    char *name;
    char *phone;
    char *dob;
};

int main()
{
    int i, student_count = 5;
    struct student ** s = malloc(sizeof(struct student *) * student_count);

    if (s)
    {
        for (i = 0; i < student_count; ++i)
        {
            s[i] = malloc(sizeof(struct student));

            if (s[i])
            {
                //set up student's members
            }
        }

        for (i = 0; i < student_count; ++i)
        {
            //free student's members before the next line.
            free(s[i]);
        }

        free(s);
    }

    return 0;
}
#包括
结构学生{
国际卷号;
字符*名称;
字符*电话;
char*dob;
};
int main()
{
int i,学生人数=5;
结构学生**s=malloc(sizeof(结构学生*)*学生人数);
若有(s)
{
对于(i=0;i
您必须
释放
所有您的
malloc
,并且如注释中所述,您不能
malloc
结构中

#include <stdio.h>
#include <stdlib.h>

#define NUM_STUDENTS 5

struct student{
    int roll_no;
    char *name;
    char *phone;
    char *dob;
};

int main(void)
{
  int i;
  // if this was me, I would simply replace this with
  // struct student s[NUM_STUDENTS];, but the goal here is to illustrate
  // malloc and free
  struct student* s = malloc(sizeof(struct student) * NUM_STUDENTS);
  if (s == NULL)  // handle error

  for (i=0; i<NUM_STUDENTS; i++)
  {
    // sizeof(char) is guaranteed to be 1, so it can be left out
    s[i].name = malloc(25);
    if (s[i].name == NULL) // handle error
    s[i].phone = malloc(10);
    if (s[i].phone == NULL) // handle error
    s[i].dob = malloc(10);
    if (s[i].dob == NULL) // handle error
  }

  // do stuff with with the data
  ....

  // time to clean up, free in the reverse order from malloc
  for (i=0; i<NUM_STUDENTS; i++)
  {
    // the dob, phone, name order here isn't important, just make sure you
    // free each struct member before freeing the struct
    free(s[i].dob);
    free(s[i].phone);
    free(s[i].name);
  }

  // now that all the members are freed, we can safely free s
  free(s);

  return 0;
}
#包括
#包括
#定义NUM_学生5
结构学生{
国际卷号;
字符*名称;
字符*电话;
char*dob;
};
内部主(空)
{
int i;
//如果这是我,我会简单地用
//struct student s[NUM_STUDENTS];,但这里的目标是说明
//malloc和free
struct student*s=malloc(sizeof(struct student)*NUM_STUDENTS);
if(s==NULL)//句柄错误

对于(i=0;i用户Abhijit给出了一个方向正确但不完整的回答。他的回答应该是:

typedef struct STUDENT{
    int roll_no;
    char *name;
    char *phone;
    char *dob;
}student;

void example(int n_students)
{
    student **s;
    int i;

    s= malloc(n_students * sizeof(student *));
    for (i=0; i<n_students; i++)
    {
        s[i]= malloc(sizeof(student));
        s[i]->name= malloc(25);
        s[i]->phone= malloc(10);
        s[i]->dob= malloc(10);
    }
    // now free it:
    for (i=0; i<n_students; i++)
    {
        free(s[i]->name);
        free(s[i]->phone);
        free(s[i]->dob);
        free(s[i]);
    }
    free(s);
}
typedef结构学生{
国际卷号;
字符*名称;
字符*电话;
char*dob;
}学生;
无效示例(国际师范大学学生)
{
学生**s;
int i;
s=malloc(n_学生*大小(学生*);
对于(i=0;iname=malloc(25);
s[i]->phone=malloc(10);
s[i]>dob=malloc(10);
}
//现在释放它:
for(i=0;iname);
免费(s[i]->电话);
自由(s[i]>dob);
免费的(s[i]);
}
免费的;
}

这不是有效的C代码。您不能在结构成员声明中执行
malloc
。此外,建议您。您的问题是什么?然后是他们..问题是什么?通常,您希望
在“镜像”中释放
项从您
malloc
ed它们的方式。也就是说,您
malloc
ed的最后一个项目应该首先被释放…就像后进先出堆栈一样。但这当然完全取决于特定的情况。“在执行我的代码之后”-我担心代码根本无法编译。我不知道在哪里为
struct student
的成员创建了任何内存。而且,在最后一个for循环
}
下,还需要一条附加语句:
免费;
:)Oops。谢谢@ryker:)Np,谢谢你的修复,谢谢你的投票。我会支持你的答案,但我已经这么做了:)。
typedef struct STUDENT{
    int roll_no;
    char *name;
    char *phone;
    char *dob;
}student;

void example(int n_students)
{
    student **s;
    int i;

    s= malloc(n_students * sizeof(student *));
    for (i=0; i<n_students; i++)
    {
        s[i]= malloc(sizeof(student));
        s[i]->name= malloc(25);
        s[i]->phone= malloc(10);
        s[i]->dob= malloc(10);
    }
    // now free it:
    for (i=0; i<n_students; i++)
    {
        free(s[i]->name);
        free(s[i]->phone);
        free(s[i]->dob);
        free(s[i]);
    }
    free(s);
}