suppose I’m passing an argument to a function like this:
void myFunc( int* arrayOfInts );
this doesn’t seem safe in the sense that the function does not know the size of the array of ints. I can put a comment saying “this function assumes it’s getting an array of 10 ints” but it is not possible to check it. So wrong usage of this function can lead to the mysterious “segmentation fault”. Is there a better way to do this?
Pass in the size of the array as well, C-style:
Or pass in a pointer to after the end of the array, iterator-style:
Or pass in a
std::vector<int>or equivalent and sidestep the problem entirely: