Fixing out-of-bounds array access
Current version reads a memory location preceding an array, and may cause undesired behavior and/or segfault's.
Issue was this line here (inside a for-loop with i=0...3
)
if (mdat[i] <= mdat[i-1]){
When i=0
, this is reading memory outside the safe range, and is going off of junk data.
Refactored the section a bit, as arrays were not necessary (several arrays were initialized, only mdat
made use of elements other than the latest one), so I turned everything into single double
's, and added an additional double, mdat_prev
, to store what would be mdat[i-1]
, and I have initialized it to zero to ensure a reasonable value is used on that first iteration, and at the end of each iteration it takes on the value of mdat
.