如何在内存中释放指针的空间?

时间:2021-09-06 19:57:48

I have question about pointers in C. Each pointer has 4 bytes in memory (address). When I call malloc() it only allocates memory and put it's address to the pointer, also free() only frees the memory that pointer is pointing to. But how can I delete pointer (4 bytes) in memory that I no longer need ? Isn't there a memory leak ?

我对C中的指针有疑问。每个指针在内存(地址)中有4个字节。当我调用malloc()时,它只分配内存并将其地址放到指针中,free()也只释放指针所指向的内存。但是如何在内存中删除不再需要的指针(4个字节)呢?是不是有内存泄漏?

5 个解决方案

#1


12  

The pointer itself is a regular variable, which means that when it goes out of scope those 4 bytes allocated for it will automatically be freed, just like any other variable you might have declared on that same scope.

指针本身是一个常规变量,这意味着当它超出范围时,将自动释放为它分配的4个字节,就像您可能在同一范围内声明的任何其他变量一样。

#2


7  

The pointer variable itself typically has automatic storage duration, i.e. it's on the stack, so the memory occupied by it will be reclaimed and reused once the variable goes out of scope.

指针变量本身通常具有自动存储持续时间,即它在堆栈上,因此一旦变量超出范围,它将占用它所占用的内存并重新使用。

#3


3  

Typically the pointer will be held in a stack variable. Or traced back through heap allocated pointers to a stack variable. The stack is statically allocated and freed when the process terminates. Thus nothing is leaked.

通常,指针将保存在堆栈变量中。或者通过堆分配指向堆栈变量的指针进行追溯。堆栈是静态分配的,并在进程终止时释放。因此没有泄露任何东西。

#4


2  

A pointer is just a normal variable, and has the same lifetime a normal variable would have in its place. I it's a local non-static variable, its lifetime ends when it goes out of scope. If it's a variable with static storage duration, its lifetime ends when the program ends. If it's located on dynamically allocated memory, its lifetime ends when that memory is freed, and for the future C1x standard, if it's a thread-local variable, its lifetime ends when the thread ends.

指针只是一个普通变量,并且具有与普通变量相同的生命周期。我是一个本地的非静态变量,它的生命周期在它超出范围时结束。如果它是具有静态存储持续时间的变量,则其生命周期在程序结束时结束。如果它位于动态分配的内存中,它的生命周期在释放该内存时结束,对于未来的C1x标准,如果它是一个线程局部变量,它的生命周期在线程结束时结束。

#5


-1  

in an nut shell..you can not free up a pointer location or memory by any code or command..for you might be wanting to make a perfect program of codes..with acute memory consumption..i get it ..but you cant.. thanks..

在一个坚果壳..你不能通过任何代码或命令释放指针位置或内存..因为你可能想要制作一个完美的代码程序..急性记忆消耗..我得到它..但你不能..谢谢..

#1


12  

The pointer itself is a regular variable, which means that when it goes out of scope those 4 bytes allocated for it will automatically be freed, just like any other variable you might have declared on that same scope.

指针本身是一个常规变量,这意味着当它超出范围时,将自动释放为它分配的4个字节,就像您可能在同一范围内声明的任何其他变量一样。

#2


7  

The pointer variable itself typically has automatic storage duration, i.e. it's on the stack, so the memory occupied by it will be reclaimed and reused once the variable goes out of scope.

指针变量本身通常具有自动存储持续时间,即它在堆栈上,因此一旦变量超出范围,它将占用它所占用的内存并重新使用。

#3


3  

Typically the pointer will be held in a stack variable. Or traced back through heap allocated pointers to a stack variable. The stack is statically allocated and freed when the process terminates. Thus nothing is leaked.

通常,指针将保存在堆栈变量中。或者通过堆分配指向堆栈变量的指针进行追溯。堆栈是静态分配的,并在进程终止时释放。因此没有泄露任何东西。

#4


2  

A pointer is just a normal variable, and has the same lifetime a normal variable would have in its place. I it's a local non-static variable, its lifetime ends when it goes out of scope. If it's a variable with static storage duration, its lifetime ends when the program ends. If it's located on dynamically allocated memory, its lifetime ends when that memory is freed, and for the future C1x standard, if it's a thread-local variable, its lifetime ends when the thread ends.

指针只是一个普通变量,并且具有与普通变量相同的生命周期。我是一个本地的非静态变量,它的生命周期在它超出范围时结束。如果它是具有静态存储持续时间的变量,则其生命周期在程序结束时结束。如果它位于动态分配的内存中,它的生命周期在释放该内存时结束,对于未来的C1x标准,如果它是一个线程局部变量,它的生命周期在线程结束时结束。

#5


-1  

in an nut shell..you can not free up a pointer location or memory by any code or command..for you might be wanting to make a perfect program of codes..with acute memory consumption..i get it ..but you cant.. thanks..

在一个坚果壳..你不能通过任何代码或命令释放指针位置或内存..因为你可能想要制作一个完美的代码程序..急性记忆消耗..我得到它..但你不能..谢谢..