C++ 的 delete

請問我若用 new 建立一個二維陣列

int **x;

int a,b;

m = new int* [3];

for(a=0;a<3;a++)

 m[a] = new int [3];

用 delete 清除的話是

for(a=0;a<3;a++)

 delete [] m[a];

delete [] m;

還是直接 delete [] m ; 就可以了?

2 Answers

Rating
  • 2 decades ago
    Favorite Answer

    for(a=0;a<3;a++)

     delete [] m[a];

    delete [] m;

    原po已經把正解寫出來了 = =

    抓個小毛病:

    x突然變成m

  • SiYu
    Lv 5
    2 decades ago

    不能直接delete []m;

    要用

    for(a=0;a<3;a++)

     delete [] m[a];

    delete [] m; 才可

Still have questions? Get your answers by asking now.