为结构体数组释放内存

6

我有一个结构体数组,之前是通过 malloc 分配的。如何释放它呢?是用 free(temp) 就可以了吗?这里的 temp 是数组的名字。还是我需要逐个释放数组元素呢?

是的。以下是方法。我添加了结构体的声明。cur_node 是一个 Node 变量。我使用 Node 创建了链表,并逐个节点释放它。

struct Node 
{
char   template_id[6];
double tm_score;
double rmsd;
double sequence_id;
int    length;
double translation_vector1;
double translation_vector2;
double translation_vector3;
double rotation_matrix1;
double rotation_matrix2;
double rotation_matrix3;
double rotation_matrix4;
double rotation_matrix5;
double rotation_matrix6;
double rotation_matrix7;
double rotation_matrix8;
double rotation_matrix9;
char   target_sequence[2000];
char   template_sequence[2000];
struct Node  *next;
};
struct Node *start_node, *cur_node;
typedef struct 
{
char   *template_id;
double tm_score;
double rmsd;
double sequence_id;
int    length;
double translation_vector1;
double translation_vector2;
double translation_vector3;
double rotation_matrix1;
double rotation_matrix2;
double rotation_matrix3;
double rotation_matrix4;
double rotation_matrix5;
double rotation_matrix6;
double rotation_matrix7;
double rotation_matrix8;
double rotation_matrix9;
char   *target_sequence;
char   *template_sequence;  
} Node1;

void traverseAlignLList()
{
  Node1 *temp;  
  struct Node *old_node;
  int temp_counter=0; 
  cur_node=start_node;
  temp=malloc(alignCounter*sizeof(Node1));
  while(cur_node!=NULL)
  {
  temp[temp_counter].template_id=malloc(6*sizeof(char));
  strcpy(temp[temp_counter].template_id,cur_node->template_id);

  temp[temp_counter].tm_score=cur_node->tm_score;
  temp[temp_counter].rmsd=cur_node->rmsd;
  temp[temp_counter].sequence_id=cur_node->sequence_id;
  temp[temp_counter].length=cur_node->length;
  temp[temp_counter].translation_vector1=cur_node->translation_vector1;
  temp[temp_counter].translation_vector2=cur_node->translation_vector2;
  temp[temp_counter].translation_vector3=cur_node->translation_vector3;
  temp[temp_counter].rotation_matrix1=cur_node->rotation_matrix1;
  temp[temp_counter].rotation_matrix2=cur_node->rotation_matrix2;
  temp[temp_counter].rotation_matrix3=cur_node->rotation_matrix3;
  temp[temp_counter].rotation_matrix4=cur_node->rotation_matrix4;
  temp[temp_counter].rotation_matrix5=cur_node->rotation_matrix5;
  temp[temp_counter].rotation_matrix6=cur_node->rotation_matrix6;
  temp[temp_counter].rotation_matrix7=cur_node->rotation_matrix7;
  temp[temp_counter].rotation_matrix8=cur_node->rotation_matrix8;
  temp[temp_counter].rotation_matrix9=cur_node->rotation_matrix9;
  temp[temp_counter].target_sequence=malloc(2000*sizeof(char));
  strcpy(temp[temp_counter].target_sequence,cur_node->target_sequence);
  temp[temp_counter].template_sequence=malloc(2000*sizeof(char));
  strcpy(temp[temp_counter].template_sequence,cur_node->template_sequence);
  temp_counter++;

  old_node=cur_node;
  cur_node=cur_node->next;
  free(old_node);
  }
  addAlignData(temp);
  free(temp);
  //free(cur_node);
  //free(start_node);
  start_node=NULL;
  }

通常情况下:每个malloc都应该有一个对应的free。 - Morten Jensen
似乎我的free(temp)没有起作用,它没有释放任何内存。我调用了这个函数很多次。 - tryu hjkl
你不应该多次调用它。那可能是危险的。每个 malloc 只调用一次即可。 - Mark Plotnick
2个回答

5
如果您使用了一个malloc来分配数组,那么一个free就足够了。
经验法则是,每个malloc都需要一个free!

7
另一个:一天一个苹果,远离医生! - ouah
@ouah 一个洋葱有什么用处? - Montaldo
4
每天吃一个洋葱,远离所有人。 - Montaldo
如果我为结构体的字段使用malloc会怎么样?看起来free(temp)没有起作用。我在Linux上。我得到了这个错误。*** glibc detected *** a.out: malloc(): memory corruption: 0x0000000005995980 *** - tryu hjkl
@tryuhjkl,请在你的问题中展示你如何进行分配。 - ouah
在这种情况下,你当然仍然需要释放,拇指规则仍然适用! - Montaldo

4

假设你有以下内容:

   int i, number_of_elements = 15;
   struct toto **array_toto;

   array_toto = malloc(sizeof(struct toto*)*number_of_elements);

   for( i = 0 ; i < number_of_elements; i++)
      array_toto[i] = malloc(sizeof(struct toto));

您需要释放:

   for( i = 0 ; i < number_of_elements; i++)
      free(array_toto[i]);
   free(array_toto);

否则你将会释放数组,但并没有释放结构体。然而,使用以下方式进行分配:
    array_toto = malloc(sizeof(struct toto)*number_of_elements);

一次免费试用就可以解决问题。

1
(*array_toto) 必须改为 array_toto - ouah
1
给点赞者:请不要投票支持那些提供有可能导致崩溃或更糟糕的代码答案。代码中的(*array_toto)使用了未初始化的指针,这是错误的。 - Eric Postpischil
我在为整个数组分配空间后,使用了mallocs来为结构体字段分配空间。这些字段最初是char*类型的。这样做会导致以后出现问题吗? - tryu hjkl
好的。我进行了调试。错误出现在以下行上。strcpy(temp[temp_counter].template_sequence,cur_node->template_sequence); - tryu hjkl
已修复。然而,这个错误并没有影响到关键答案,即如果结构体是单独分配的,则应该释放它们本身。 - Sebastien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接