2010-10-19 126 views
0

現在我有一個指針設置爲我的二維數組中的一行。我希望該指針停止指向該行,但我將在稍後使用指針來進行其他操作。我只想知道如何在指針初始化並指向一行後取消設置。如何在運行時將指針設置爲空(C++)

double* tempRow; 
tempRow = (double*) malloc(size * sizeof(double)); 
    ... 
tempRow = NULL; 

不會取消將tempRow變量與數組行相鏈接。爲什麼不?

不知我是否應該用C來代替。使用矢量時會有開銷嗎?

+3

那麼,你從學習這本書嗎?這是非常糟糕的C++代碼,很簡單,並且被任何[入門書]所覆蓋(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。您應該選擇一個,以便您可以學習C++。答案當然是使用'std :: vector '。 「看起來不起作用」爲 – GManNickG 2010-10-19 06:14:13

+1

-1。 – 2010-10-19 07:31:24

+0

似乎不起作用的是指代碼上面的句子。即使用提供的代碼「在初始化指針並指向一行後取消設置指針......似乎不起作用。」 – Stuart 2010-10-19 16:04:53

回答

4

似乎不工作?

這是最糟糕的投訴和解決方案提供商的噩夢。

你的意思是你得到一個編譯錯誤?

如果是的話,你有沒有包括<cstdio>?using namespace std;

2

不以何種方式工作?正常途徑「未設置」 C++中的指針是:

tempRow = 0; 

,但你必須要細,假設你已經包括了正確的頭或其他什麼有NULL正確的定義。另外,在丟失指針之前,你應該首先調用free(),否則你會發生內存泄漏(並且這是假設你有一個很好的理由使用C風格malloc/free而不是更多的Kosher C++ new/delete)。

11

雖然你已經寫了將tempRow設置爲NULL,它不會釋放你分配的內存。爲此你需要

free(tempRow); 
tempRow = NULL; 

但是如果你使用C++作爲標籤建議,你會更好使用C++新/刪除

double* tempRow; 
tempRow = new double[size]; 
    ... 
delete [] tempRow; 
tempRow = NULL; 

,你甚至可以使用STL處理您爲你分配內存。

std::vector<double> tempRow(size); 
// You can access the data, in a similar way to an array 
tempRow[5] = tempRow[4]+tempRow[3]; 

// If you really need to access the underlying pointer, (To pass to another 
// function for example) you can do this. Note that the pointer will be valid 
// until the vector is destroyed or modified in certain ways that can cause the 
// vector to reallocate its memory. So you can't use this to pass data to a 
// function that destroys or takes ownership of the passed in pointer. 

fn_requiring_pointer(&temp[0]); 

// The memory used in tempRow will get cleaned up automatically when the 
// object goes out of scope 
// 
// If I really need to free up the memory used in it early I can use a swap 
// hack. (iirc tempRow.clear() isn't guaranteed to release the memory) 
std::vector<double>().swap(tempRow); // Unneeded in most cases. 

也試圖重用tempRow指針的東西不相關可能沒有必要。只需創建一個不同名稱的新指針。重複使用變量形式多個不同的不相關的目的可能會使代碼很難在以後明白。

+6

我聽說'std :: vector tempRow(size)'是使用'new []'和'delete []'的好替代方法,但我是C++ noob,因此請帶上鹽。 – dreamlax 2010-10-19 06:10:04

+1

同意std :: vector 往往更好,如果你不真的需要指針。添加到答覆中。 – 2010-10-19 06:12:28

+0

@ dreamlax這是一個很好的建議,因爲矢量處理內存。 – Rudi 2010-10-19 06:22:08

4

您顯示的示例應該可以工作。
此外,如果你沒有做temRowNULL之前釋放的內存,你是泄漏內存。

double* tempRow; 
tempRow = (double*) malloc(size * sizeof(double)); 
    ... 
free(tempRow); // free the memory. 
tempRow = NULL; // reset the pointer. 
    ... 
tempRow = &some_other_double_var; // reuse the pointer. 
+0

我對內存分配了解不多。不釋放只是未分配內存?因爲那不是我想要的。我希望稍後在程序中重用內存。我可以釋放並設置爲NULL,但是隨後我不得不在每次使用它時重新分配指針,我寧願避免開銷。也許我誤解了,因爲在你的例子中,你正在釋放,設置爲NULL,然後在沒有malloc調用的情況下重用它。它是否正確? – Stuart 2010-10-19 16:13:37

+0

@Stuart:當您將指針設置爲NULL並且您不想免費調用時,您需要確保有另一種方法可以分配內存。假設你可能有另一個指向它的指針。如果你沒有這樣的方式,那麼你正在泄漏內存,那就是沒有辦法去解決這個問題。 – codaddict 2010-10-19 16:20:37

+0

@Stuart:很難理解你在問什麼。 'free()'釋放內存。如果你想保留內存供以後重用,你需要保留一個指針。如果你做'tempRow = NULL;'沒有'free(tempRow);'首先,你會得到最糟糕的兩個:不可重用的內存,甚至不能重新分配。順便說一句,你可能會做得更好,只是「釋放內存並在以後獲取新內存」。開銷不太可能會那麼糟糕,而且你似乎並不熟悉內存分配。 – 2010-10-19 16:24:37

6

我在C新型++爲好,但前一陣子,有人告訴我,使用std::vector是處理數據的陣列,更安全的方法。

  • 添加更多元素時自動重新分配。
  • 迭代器與#include <algorithm>的東西一起使用。
  • 邊界保護與.at(index)元素訪問。
  • 沒有雜亂的指針跟蹤需要。
  • C型陣列樣式訪問operator[]
  • RAII。

你將宣佈這樣的載體:

std::vector<double> tempRow(size); 

tempRow[0] = 3.00; 
tempRow[1] = 1.00; 

// no need to use delete[] or free(), it will destruct itself 
// and relinquish its resources automatically.