Proper way to pass dynamic arrays to other functions

后端 未结 5 1170
再見小時候
再見小時候 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:11

    Use this:

    void myFuncThatAcceptsDynamicArrays(bool* array, int size) {
       // Do something (using the size as the size of the array)
    }
    

    It is up to the user of the function to provide a valid size (which can be very dangerous).

提交回复
热议问题