when do we need to pass the size of array as a parameter

后端 未结 8 2090
青春惊慌失措
青春惊慌失措 2020-12-01 19:00

I am a little bit confused about pass an array in C/C++. I saw some cases in which the signature is like this

void f(int arr[])

some is lik

8条回答
  •  醉梦人生
    2020-12-01 19:38

    The first signature just passes the array with no way to tell how big the array is and can lead to problems with out-of-bounds errors and/or security flaws.\

    The second signature is a more secure version because it allows the function to check against the size of the array to prevent the first versions shortcomings.

    Unless this is homework, raw arrays are a bit out-dated. Use std::vector instead. It allows passing the vector around without having to manually pass the size as it does this for you.

提交回复
热议问题