2013-02-18 88 views
3

所以,我有這個代碼,我試圖在最後釋放數組ppint。我曾嘗試在Xcode中使用泄漏來確定它是否正常工作,但我不太明白。將這樣做的工作?拆分動態分配的多維數組。

delete ppint[0]; 
delete ppint[1]; 
delete ppint[2]; 
delete ppint[3]; 

或者還有其他的事情必須完成嗎?

#include <iostream> 
#include <string> 
#include <unistd.h> 
using namespace std; 

int main() 
{ 
    int **ppint; 
    ppint = new int * [4]; 

    for(int i = 0; i < 4; i++) { 
     ppint [i] = new int[4]; 
    } // declares second layer of arrays 

    for(int i = 0, count = 0; i < 4; i++) { 
     for(int j = 0; j < 4; j++) { 
      count++; 
      ppint [i] [j] = count; 
     } //init part 2 
    } // init array 

    for(int i = 0; i < 4; i++) { 
     for(int j = 0; j < 4; j++) { 
      cout << ppint [i] [j] << endl; 
     } // print part 2 
    } //print array 
} 
+1

任何特殊原因不使用std :: vector >? – piokuc 2013-02-18 20:53:53

+0

@piokuc我正在寫一本書,說這樣做,只有真正的原因,除了我沒有使用過的矢量。 – MarJamRob 2013-02-18 20:54:57

+1

記住:每個'new'都應該有一個相應的'delete',每個'new''都應該有一個相應的'delete []'。 – Cameron 2013-02-18 20:55:40

回答

6

關閉。你將不得不使用delete[]因爲你new[]分配它們:

delete[] ppint[0]; 
delete[] ppint[1]; 
delete[] ppint[2]; 
delete[] ppint[3]; 

不過,當然,你應該使用一個循環:

for(int i = 0; i < 4; i++) { 
    delete[] ppint[i]; 
} 

然後不要忘記delete[]ppint本身:

delete[] ppint; 

但是,在C++中,我們更傾向於不動用動態分配的數組。使用std::vector<std::vector<int>>std::array<std::array<int, 4>, 4>。如果您關心數據的位置,請嘗試boost::multi_array

1

您仍然需要刪除ppint本身指向的內存。您還需要使用delete[]而不是delete

但是,比手動數組更喜歡標準容器。與new[]需要分配

std::vector< std::vector<int> > iv; // dynamic size 
std::array< std::array<int,4>, 4> ia; // static size 
1

一件用delete[]被釋放。但使用std::vector而不是原始數組和new。它自動爲您管理內存。


下模擬直接在你的原代碼,以及更短,更清潔,更安全:

#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
    vector<vector<int>> v(4, vector<int>(4)); 

    for(int i = 0, count = 0; i < 4; ++i) { 
     for(int j = 0; j < 4; ++j) { 
      ++count; 
      v[i][j] = count; 
     } 
    } 

    for(int i = 0; i < 4; ++i) { 
     for(int j = 0; j < 4; ++j) { 
      cout << v[i][j] << endl; 
     } 
    } 
} 

更在C++中,你可以/應該定義一個可重複使用的矩陣類,例如精神

#include <iostream> 
#include <vector> 
using namespace std; 

template< class item_t > 
class matrix_ 
{ 
private: 
    vector<item_t> items_; 
    int    width_; 
    int    height_; 

    int index_for(int const x, int const y) const 
    { 
     return y*width_ + x; 
    } 

public: 
    item_t const& operator()(int const x, int const y) const 
    { 
     return items_[index_for(x, y)]; 
    } 

    item_t& operator()(int const x, int const y) 
    { 
     return items_[index_for(x, y)]; 
    } 

    matrix_(ssize_t const w, ssize_t const h) 
     : items_(w*h) 
     , width_(w) 
     , height_(h) 
    {} 
}; 

int main() 
{ 
    matrix_<int> m(4, 4); 

    for(int i = 0, count = 0; i < 4; ++i) { 
     for(int j = 0; j < 4; ++j) { 
      ++count; 
      m(j, i) = count; 
     } 
    } 

    for(int i = 0; i < 4; ++i) { 
     for(int j = 0; j < 4; ++j) { 
      cout << m(j, i) << endl; 
     } 
    } 
} 
1

你需要一個呼叫delete每個早期版本調用new

for(int i = 0; i < 4; i++) { 
    delete[] ppint[i]; 
} 
delete[] ppint; 
+2

re'delete ppint;',用'new []'分配的東西需要用'delete []'來解除分配。 – 2013-02-18 21:00:54

+0

謝謝。糾正其他刪除後,我馬上就會怠慢 – simonc 2013-02-18 21:18:50

2

我的解決辦法是:

#include<iostream> 
#include<cstdlib> 

using namespace std; 

int main(){ 

    int ** twod; 
    twod = new int*[4]; 
    int counter = 0; 
    /*init 2d variable and check whether we got the memory*/ 
    if (twod == NULL) { 
     exit(EXIT_FAILURE); 
    } 
    for (unsigned i = 0; i< 4; i++){ 
     /**/ 
     twod[i] = new int[4]; 
     if (twod[i] == NULL){ 
      exit(EXIT_FAILURE); 
     } 
     for (unsigned j = 0; j < 4; j++){ 
      counter++; 
      twod[i][j]=counter; 
     } 
    } 

    for (unsigned i = 0; i < 4; i++){ 
     for (unsigned j = 0; j < 4; j++){ 
      cout << twod[i][j] << endl ; 
     } 
    } 

    for (unsigned i = 0; i < 4; i++) 
     delete [] twod[i]; 

    /*and don't forget to delete the int* array as well.*/ 
    delete [] twod; 

} 

如果你想確保你沒有做任何內存錯誤也使用valgrind:

Valgrind是一款出色的工具用於檢測內存錯誤。在輸出中顯示我們做了5個內存分配,這些分配都被釋放了。 Valgrind還可以顯示其他類型的內存錯誤,例如使用你根本沒有分配的內存。使用在使用前沒有正確初始化的內存,就像我說的一個優秀的內存檢查工具。

$ valgrind ./a.out 
==18376== Memcheck, a memory error detector 
==18376== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. 
==18376== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info 
==18376== Command: ./a.out 
==18376== 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
==18376== 
==18376== HEAP SUMMARY: 
==18376==  in use at exit: 0 bytes in 0 blocks 
==18376== total heap usage: 5 allocs, 5 frees, 96 bytes allocated 
==18376== 
==18376== All heap blocks were freed -- no leaks are possible 
==18376== 
==18376== For counts of detected and suppressed errors, rerun with: -v 
==18376== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) 

當然,其他人告訴你使用std :: vector例如你在C++中編碼而不是c。

+0

您是對的我的代碼確實會產生不同的輸出,但我試圖專注於OP的內存問題。 – hetepeperfan 2013-02-19 09:16:55

+0

我調整了我的答案以獲得與OP相同的輸出 – hetepeperfan 2013-02-20 10:06:33