[英]Deleting a pointer in C++
背景:我試圖圍繞指針,我們幾周前在學校看到它們,今天練習時我遇到了傻瓜? 問題,對你來說可能非常簡單,但我幾乎沒有編程經驗。
我在SO中看到了很多關於刪除指針的問題,但它們似乎都與刪除一個類有關,而不是一個“簡單”指針(或任何正確的術語),這里是我試圖的代碼跑:
#include <iostream>;
using namespace std;
int main() {
int myVar,
*myPointer;
myVar = 8;
myPointer = &myVar;
cout << "delete-ing pointers " << endl;
cout << "Memory address: " << myPointer << endl;
// Seems I can't *just* delete it, as it triggers an error
delete myPointer;
cout << "myPointer: " << myPointer << endl;
// Error: a.out(14399) malloc: *** error for object 0x7fff61e537f4:
// pointer being freed was not allocated
// *** set a breakpoint in malloc_error_break to debug
// Abort trap: 6
// Using the new keyword befor deleting it works, but
// does it really frees up the space?
myPointer = new int;
delete myPointer;
cout << "myPointer: " << myPointer << endl;
// myPointer continues to store a memory address.
// Using NULL before deleting it, seems to work.
myPointer = NULL;
delete myPointer;
cout << "myPointer: " << myPointer << endl;
// myPointer returns 0.
}
所以我的問題是:
很抱歉這個問題很長,想要盡可能清楚,也要重申,我的編程經驗很少,所以如果有人能用外行的條款回答這個問題,我們將不勝感激!
1和2
myVar = 8; //not dynamically allocated. Can't call delete on it.
myPointer = new int; //dynamically allocated, can call delete on it.
第一個變量在堆棧上分配。 您只能在使用new
運算符動態分配的內存(在堆上)上調用delete。
3。
myPointer = NULL;
delete myPointer;
以上什么都沒做 。 你沒有釋放任何東西,因為指針指向NULL。
不應該做以下事情:
myPointer = new int;
myPointer = NULL; //leaked memory, no pointer to above int
delete myPointer; //no point at all
你把它指向NULL,留下泄漏的內存(你分配的新int)。 你應該釋放你指向的記憶。 沒有辦法再訪問分配的new int
,因此內存泄漏。
正確的方法:
myPointer = new int;
delete myPointer; //freed memory
myPointer = NULL; //pointed dangling ptr to NULL
更好的方法:
如果您使用的是C ++, 請不要使用原始指針。 使用智能指針 ,它可以為您處理這些事情,而且開銷很小。 C ++ 11附帶了幾個 。
我相信你並沒有完全理解指針是如何工作的。
如果指針指向某個內存,則必須了解三個不同的內容:
- 指針(內存)有“指向的東西”
- 這個內存地址
- 並非所有指針都需要刪除內存:您只需要刪除動態分配的內存(使用new
運算符)。
想像:
int *ptr = new int;
// ptr has the address of the memory.
// at this point, the actual memory doesn't have anything.
*ptr = 8;
// you're assigning the integer 8 into that memory.
delete ptr;
// you are only deleting the memory.
// at this point the pointer still has the same memory address (as you could
// notice from your 2nd test) but what inside that memory is gone!
當你做到了
ptr = NULL;
// you didn't delete the memory
// you're only saying that this pointer is now pointing to "nowhere".
// the memory that was pointed by this pointer is now lost.
C ++允許您嘗試delete
指向null
的指針但它實際上沒有做任何事情,只是不會給出任何錯誤。
指針與普通變量類似,因為您不需要刪除它們。 它們在函數執行結束時和/或程序結束時從內存中刪除。
但是,您可以使用指針來分配內存的“塊”,例如:
int *some_integers = new int[20000]
這將為20000個整數分配內存空間。 很有用,因為Stack的大小有限,你可能想要在沒有堆棧溢出錯誤的情況下加入大量的'int'。
無論何時調用new,都應該在程序結束時“刪除”,否則會出現內存泄漏,並且永遠不會返回一些已分配的內存空間供其他程序使用。 去做這個:
delete [] some_integers;
希望有所幫助。
在C ++中有一條規則,對於每一個新的都有刪除 。
永遠不會被稱為新的。 因此,cout打印的地址是myVar的內存位置的地址,或者在這種情況下分配給myPointer的值。 通過寫:
myPointer = &myVar;
你說:
myPointer = myVar中數據存儲位置的地址
它返回一個指向已刪除的內存位置的地址。 因為首先創建指針並將其值分配給myPointer,然后再將其刪除,第三步將其打印出來。 因此,除非您為myPointer指定其他值,否則將保留已刪除的地址。
NULL等於0,刪除0,因此不刪除任何內容。 它的邏輯是打印0因為你做了:
myPointer = NULL;
等於:
myPointer = 0;
int value, *ptr;
value = 8;
ptr = &value;
// ptr points to value, which lives on a stack frame.
// you are not responsible for managing its lifetime.
ptr = new int;
delete ptr;
// yes this is the normal way to manage the lifetime of
// dynamically allocated memory, you new'ed it, you delete it.
ptr = nullptr;
delete ptr;
// this is illogical, essentially you are saying delete nothing.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.