學習筆記 動態二維陣列的建立 釋放 及傳參

2021-10-04 12:43:55 字數 4059 閱讀 5045

@c/c++學習-二維動態陣列的建立,與傳參

利用malloc,free 建立及釋放二維陣列;

本文以簡單的檢驗二維矩陣是否為幻方矩陣的例題為例。幻方矩陣即每行/每主對角線/每副對角線的元素和均相等.

其中建立二維動態數字的部分為:
`int n =0;

scanf

("%d"

,&n)

;int

**array =

(int**

)malloc

(sizeof

(int

)* n * n)

;//n階方陣

assert

(array !=

null);

memset

(array,0,

sizeof

(int

)* n * n)

;//這句其實沒用

for(

int i =

0; i < n; i++

)`

我用了assert()做對申請空間失敗的檢測;(這種方法是我一開始使用的方法,後來發現是錯的,詳情見即new delete 語句對不應該用assert()判斷。因為new 語句不會返回null,應該用

try

catch

(bad_alloc)

catch(.

..)

另外,assert函式僅適用於除錯階段,發布階段不適用,因此不適合用於此處對空指標的檢測,以避免產品上線後可能產生的bug.

最基礎的

```c

if(null == array)

有它的價值。

二維動態陣列的釋放也是要遵循規律,先釋放迴圈體內的,再釋放最大二維陣列。

```c

for (int i = 0; i < n; i++)

free(array);

array = null;

釋放之後指標置空防止野指標。

//《c++程式設計實踐教程》

//exp12_6 by 隨志同 2020-3-28

//判定n*n的矩陣是否為幻方

//每行/每主對角線/每副對角線 元素和相等

//編譯器:g++ (x86_64-posix-seh-rev0, built by mingw-w64 project) 8.1.0

#include

#include

#include

intis_huanfang

(int

**array,

const

int arraysize)

;int

is_huanfang

(int

**array,

const

int arraysize)if(

(i + j +1)

== arraysize)

}printf

("\n");

}for

(int i =

0; i < arraysize; i++

)printf

("sum_line1: %d \n"

,sum_line1)

;printf

("sum_line2: %d \n"

,sum_line2);if

(sum_line2 == sum_line1)

}free

(sum_row)

;return1;

}else

}int

main()

for(

int i =

0; i < n; i++

)getchar()

;//這句為了解決輸入時scanf會讀入\n的問題

}printf

("矩陣為:");

for(

int i =

0; i < n; i++

)printf

("\n");

}printf

("\n");

if(is_huanfang

(array, n)

)else

for(

int i =

0; i < n; i++

)free

(array)

; array =

null

;system

("pause");

return0;

}

c++對2維動態陣列的建立我只會一下兩種方法

與malloc -free 相對應

**如下:

不得不說,cin,cout 確實比printf(),sacnf()用起來方便。

//exp12_6   by 隨志同   2020-3-27

//判定n*n的矩陣是否為幻方

//每行/每主對角線/每副對角線 元素和相等

#include

#include

#include

#include

#include

using std::cin;

using std::cout;

using std::endl;

using std::vector;

intis_huanfang

(int

**array,

const

int arraysize)

;int

is_huanfang

(int

**array,

const

int arraysize)

;int sum_line1 =0;

int sum_line2 =0;

for(

int i =

0; i < arraysize; i++)if

((i + j +1)

== arraysize)

} cout << endl;}if

(sum_line2 == sum_line1)

}delete

sum_row;

return1;

}else

}int

main()

for(

int i =

0; i < n; i++)}

cout <<

"test_array"

<< endl;

for(

int i =

0; i < n; i++

) cout << endl;

} cout <<

"testover"

<< endl;if(

is_huanfang

(array, n)

)else

for(

int i =

0; i < n; i++

)delete

array;

array =

null

;system

("pause");

return0;

}

vector形式的容器用來構建二維動態「陣列」是最方便的。但我還沒學好,**後補;

我常見的自定義函式示例

int

fun(

int*

* array,

int*arraycolsize,

int arraysize)

;

該形式一般是用,二維陣列的指標作為形參進行傳遞,其中*arraysize引數我看leetcode上是表示每行的長度。arraysize引數一般表示,陣列大小n,或者其他形式。由程式設計師自己規定。

另有const int ** array 做形參保護array不被改變,我暫時還不熟練掌握,以後再做擴充套件。

另本人系初學者,如有錯漏之處,萬請告知,再次拜謝。

動態建立和釋放二維陣列

c動態建立和釋放二維陣列 include include define row 5 define col 4 main int i int arr arr int malloc row sizeof int for i 0 i使用calloc申請記憶體時,記憶體會清0,而malloc並不進行這項工作...

動態建立和釋放二維陣列

define crt secure no warnings include include include 動態建立二維陣列,指標做輸出 int get2darr char arr out int row,int col for int i 0 i row i arr p 掛上 return 0 完...

動態建立二維陣列

c 中用new動態建立二維陣列的格式一般是這樣 type p n new type n 其中,type是某種型別,n是二維陣列的列數。採用這種格式,列數必須指出,而行數無需指定。在這裡,p的型別是type n 即是指向乙個有n列元素陣列的指標。還有一種方法,可以不指定陣列的列數 int p p ne...