你将如何释放分配的内存?

时间:2021-02-27 21:17:08

I need to free some memory allocated on my program. Can I use something to clean the memory when I need it ?

我需要释放一些在程序中分配的内存。我需要时可以用东西清理内存吗?

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}

2 个解决方案

#1


0  

It's simple. You can use this code to clean your only used variable.

这很简单。您可以使用此代码清除您唯一使用的变量。

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
     int **p, i, j;
     p = (int **) malloc(MAXROW * sizeof(int*)); 
     free(p);
     return 0;
}

#2


5  

Point 1

第1点

You cannot free some memory. You have to free all. To elaborate, whatever memory has been allocated by a single call to malloc() or family, will be free-d at a time. You cannot free half (or so) of the allocated memory.

你不能释放一些记忆。你必须释放所有。详细说明,通过单次调用malloc()或family分配的内存,一次只能*调用。你不能释放一半(或左右)分配的内存。

Point 2

第2点

  • Please do not cast the return value of malloc() and family in C.
  • 请不要在C中强制转换malloc()和family的返回值。
  • You should always write sizeof(*ptr) instead of sizeof(type*).
  • 你应该总是写sizeof(* ptr)而不是sizeof(type *)。

Point 3

第3点

You can use the free() to free the allocated memory.

您可以使用free()释放分配的内存。

for example, see the below code and please notice the inline comments.

例如,请参阅以下代码,请注意内联注释。

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main(void)                        //notice the signature of main
{
    int **p = NULL;                      //always initialize local variables
    int i = 0, j = 0;

    p = malloc(MAXROW * sizeof(*p));  // do not cast and use sizeof(*p)
    if (p)                            //continue only if malloc is a success
    {

        //do something
        //do something more

        free(p);                      //-----------> freeing the memory here.
    }
    return 0;
}

#1


0  

It's simple. You can use this code to clean your only used variable.

这很简单。您可以使用此代码清除您唯一使用的变量。

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
     int **p, i, j;
     p = (int **) malloc(MAXROW * sizeof(int*)); 
     free(p);
     return 0;
}

#2


5  

Point 1

第1点

You cannot free some memory. You have to free all. To elaborate, whatever memory has been allocated by a single call to malloc() or family, will be free-d at a time. You cannot free half (or so) of the allocated memory.

你不能释放一些记忆。你必须释放所有。详细说明,通过单次调用malloc()或family分配的内存,一次只能*调用。你不能释放一半(或左右)分配的内存。

Point 2

第2点

  • Please do not cast the return value of malloc() and family in C.
  • 请不要在C中强制转换malloc()和family的返回值。
  • You should always write sizeof(*ptr) instead of sizeof(type*).
  • 你应该总是写sizeof(* ptr)而不是sizeof(type *)。

Point 3

第3点

You can use the free() to free the allocated memory.

您可以使用free()释放分配的内存。

for example, see the below code and please notice the inline comments.

例如,请参阅以下代码,请注意内联注释。

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main(void)                        //notice the signature of main
{
    int **p = NULL;                      //always initialize local variables
    int i = 0, j = 0;

    p = malloc(MAXROW * sizeof(*p));  // do not cast and use sizeof(*p)
    if (p)                            //continue only if malloc is a success
    {

        //do something
        //do something more

        free(p);                      //-----------> freeing the memory here.
    }
    return 0;
}