使用malloc释放c中分配的空间

时间:2021-10-12 21:19:32

I have a question about this code:

我对此代码有疑问:

typedef struct pop {
unsigned long int *np; // matrix
unsigned long int f;
long double fp; 
unsigned long int *R; // matrix
unsigned long int *C; // matrix
unsigned long int Dp;
unsigned long int Ds;
unsigned long int count;
struct popolazione *ptrTempLst; // pointer
struct popolazione *leftTree;  // left tree pointer
struct popolazione *rightTree; // right tree pointer
} Node; 

When I free space allocated for this struct, prior have I to free pointer to matrix inside struct?

当我释放为这个结构分配的空间时,先前我要将结构中的指针释放到指针吗?

For example,

例如,

 Node *ptr=(Node *) malloc(sizeOf(Node));
 ptr->np=(unsigned long int *)malloc(10*sizeOf(unsigned long int));

 /*code code code*/

 // is necessary: free(ptr->np); 

 free(ptr);

Thanks in advance

提前致谢

2 个解决方案

#1


6  

That is correct.

那是对的。

To help avoid shooting yourself in the foot, you might consider the following practices:

为了避免自己在脚下射击,您可以考虑以下做法:

  1. Always free all malloc/calloc'ed memory with free()
  2. 始终释放所有malloc / calloc'ed内存与free()
  3. Afterwards, set the pointer to NULL
  4. 然后,将指针设置为NULL
  5. Use a dedicated cleanup/destroy function to ensure consistent memory cleanup
  6. 使用专用的清理/销毁功能来确保一致的内存清理

The following function would be a good way to make sure you always cleanup a structure properly, avoid memory leaks, and avoid accidentally freeing already-freed memory and causing a segmentation fault:

以下函数是确保始终正确清理结构,避免内存泄漏,并避免意外释放已释放的内存并导致分段错误的好方法:

int destroyNode(Node* myNode) {
  if(!myNode) {
    printf("Invalid pointer! Exiting");
    return (-1);
  }

  // Clear out memory
  if(np) {
    free(np);
    np = NULL;
  }
  if(R) {
    free(R);
    R = NULL;
  }
  if(C) {
    free(C);
    C = NULL;
  }
  if(ptrTempLst) {
    free(ptrTempLst);
    ptrTempLst = NULL;
  }
  if(leftTree) {
    free(leftTree);
    leftTree = NULL;
  }
  if(rightTree) {
    free(rightTree);
    rightTree = NULL;
  }

  free(myNode);
}

eg:

例如:

int main(void) {
  Node *tempNode = calloc((size_t)1,sizeof(Node));

  // Alloc the member nodes, etc, do some code


  // Ready to clean up and exit program
  destroyNode(tempNode);
  tempNode = NULL;

  return 0;
}

Good luck!

祝你好运!

#2


9  

Yes.

是。

Every call to malloc must have a matching call to free.

每次调用malloc都必须有一个匹配的免费调用。

#1


6  

That is correct.

那是对的。

To help avoid shooting yourself in the foot, you might consider the following practices:

为了避免自己在脚下射击,您可以考虑以下做法:

  1. Always free all malloc/calloc'ed memory with free()
  2. 始终释放所有malloc / calloc'ed内存与free()
  3. Afterwards, set the pointer to NULL
  4. 然后,将指针设置为NULL
  5. Use a dedicated cleanup/destroy function to ensure consistent memory cleanup
  6. 使用专用的清理/销毁功能来确保一致的内存清理

The following function would be a good way to make sure you always cleanup a structure properly, avoid memory leaks, and avoid accidentally freeing already-freed memory and causing a segmentation fault:

以下函数是确保始终正确清理结构,避免内存泄漏,并避免意外释放已释放的内存并导致分段错误的好方法:

int destroyNode(Node* myNode) {
  if(!myNode) {
    printf("Invalid pointer! Exiting");
    return (-1);
  }

  // Clear out memory
  if(np) {
    free(np);
    np = NULL;
  }
  if(R) {
    free(R);
    R = NULL;
  }
  if(C) {
    free(C);
    C = NULL;
  }
  if(ptrTempLst) {
    free(ptrTempLst);
    ptrTempLst = NULL;
  }
  if(leftTree) {
    free(leftTree);
    leftTree = NULL;
  }
  if(rightTree) {
    free(rightTree);
    rightTree = NULL;
  }

  free(myNode);
}

eg:

例如:

int main(void) {
  Node *tempNode = calloc((size_t)1,sizeof(Node));

  // Alloc the member nodes, etc, do some code


  // Ready to clean up and exit program
  destroyNode(tempNode);
  tempNode = NULL;

  return 0;
}

Good luck!

祝你好运!

#2


9  

Yes.

是。

Every call to malloc must have a matching call to free.

每次调用malloc都必须有一个匹配的免费调用。