Proper way to pass dynamic arrays to other functions

后端 未结 5 1154
再見小時候
再見小時候 2020-12-16 21:31

What\'s the most \"proper\" way to pass a dynamically sized array to another function?

bool *used = new bool[length]();

I\'ve come up with

5条回答
  •  遥遥无期
    2020-12-16 22:26

    static void test(bool arr[])
    static void test(bool *arr, size_t size)
    

    For static/dynamic arrays, if you don't want to change location of this pointer.

    Example: http://liveworkspace.org/code/c5e379ebe2a051c15261db05de0fc0a9

    static void test(bool *&arr)
    

    For dynamic if you want to change location.

    Example: http://liveworkspace.org/code/bd03b214cdbe7c86c4c387da78770bcd

    But, since you write on C++ - use vectors, instead of raw dynamic arrays.

提交回复
热议问题