【问题标题】:Free memory for array of structs结构数组的可用内存
【发布时间】:2013-11-06 21:35:30
【问题描述】:

我有一个以前由 malloc 分配的结构数组。 释放它: 免费(临时)可以吗? temp 是数组的名称。 还是我应该逐个元素地释放元素?

是的。这个函数。好吧,我添加了structs.cur_node的声明是Node的一个var。我使用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
  • 我的 free(temp) 似乎无法正常工作。它没有释放任何内存。我多次调用这个函数。
  • 你不应该多次调用它。那可能很危险。只需为每个 malloc 调用一次。

标签: c


【解决方案1】:

如果你使用一个malloc来分配数组,一个free就足够了。

经验法则,每个 malloc 都需要一个免费的!

【讨论】:

  • 还有一个:一天一苹果,医生远离我!
  • @ouah 那么洋葱有什么作用呢?
  • 一天一个洋葱,让大家远离
  • 如果我将 malloc 用于 struct 的字段怎么样?看来我的 free(temp) 不起作用。我在 Linux 上。我收到此错误。 *** 检测到 glibc *** a.out: malloc(): 内存损坏:0x0000000005995980 ***
  • @tryuhjkl 在您的问题中显示您是如何进行分配的。
【解决方案2】:

假设你有:

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

一个免费的就可以了。

【讨论】:

  • (*array_toto) 必须是 array_toto
  • 致投票者:请不要投票给提供可能崩溃或更糟的代码的答案。代码中的(*array_toto) 使用了未初始化的指针。这是错误的。
  • 在为整个数组占用空间后,我将 mallocs 用于结构字段。这些字段最初是 char* s。会不会导致以后出现问题?
  • 好吧。我调试了。错误出现在下一行。 strcpy(temp[temp_counter].template_sequence,cur_node->template_sequence);
  • 已修复。尽管如此,这个错误并没有分散关键答案的注意力,即如果单独分配结构本身应该被释放。
猜你喜欢
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-06
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 2013-06-12
相关资源
最近更新 更多