结构数组的可用内存

try*_*jkl 5 c

我有一个之前由 malloc 分配的结构数组。释放它:free(temp) 可以吗?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;
  }
Run Code Online (Sandbox Code Playgroud)

Mon*_*ldo 5

如果您使用 1 个 malloc 来分配数组,则 1 个 free 就足够了。

根据经验,每个 malloc 都需要一个 free!

  • 另:一天一苹果,医生远离我! (7认同)
  • 一天一粒洋葱,让所有人远离你 (4认同)

Seb*_*ien 5

假设您有:

   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));
Run Code Online (Sandbox Code Playgroud)

你必须释放:

   for( i = 0 ; i < number_of_elements; i++)
      free(array_toto[i]);
   free(array_toto);
Run Code Online (Sandbox Code Playgroud)

否则,您将释放数组而不是结构。然而,分配:

    array_toto = malloc(sizeof(struct toto)*number_of_elements);
Run Code Online (Sandbox Code Playgroud)

一个免费的就可以了。