[C++筆記]

Samuel Liu
Feb 6, 2021

--

Array當成參數傳入時,傳的是起始的位址,因此想要check傳入的array的長度這種想法是不對的。 ref: https://stackoverflow.com/questions/33041248/c-array-as-function-parameter-size-check

Assert 在 DEBUG時扮演校驗的角色。 ref: https://www.itread01.com/article/1499140307.html

得Array長度的方法為

int arr[5];
int size = sizeof(arr) / sizeof(arr[0])
// Or sizeof(arr) / sizeof(int)
// then we get size == 5

解決"function returns address of local variable" error:在function中local variable的空間會在function執行完畢時回收空間,因此今天如果是要回傳一個local的pointer變數,是不合法的行為。

解決方法為傳入pointer為參數來進行function內要做的事。

ref: https://www.educative.io/edpresso/resolving-the-function-returns-address-of-local-variable-error

C的array並不存在boundary check之功能,因此工程師必須注意是否會access undefined space。當access undefined space時會發生 (a) 不是預期的值 (b) Segmentation Fault: access到一些非法的區域

C++中的vector則內建boundary checking

ref: https://www.geeksforgeeks.org/accessing-array-bounds-ccpp/

std::array之用法

--

--

No responses yet