There are a couple of problems with undefined behavior in your code:
The first is that you pass 9
as high
which will be used to index the tenth element of an eight-element array. It will be the tenth because in cross_max
you loop while i <= high
, so you will index arr[9]
. Remember that array indexes are from zero to the size minus one (so you can index from 0
to 7
for your array). The indexes out of bounds will contain undefined (i.e. random) values.
The second problem is that you are returning pointers to a local variable from cross_max
. This will lead to undefined behavior when you use that returned pointer. Local variables are only valid inside the scope they were declared, and when the function returns the memory area used by the local variables will be reclaimed and used for the next function.