如何在內存中釋放指針的空間?

[英]How to free pointer's space in memory?


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..

在一個堅果殼..你不能通過任何代碼或命令釋放指針位置或內存..因為你可能想要制作一個完美的代碼程序..急性記憶消耗..我得到它..但你不能..謝謝..


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/07/03/90a986029f960f9e352706ca02d5389d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com