Array to big for ESP8266 12F

I am using the following code in order to get rid of the runaway values of a distance measurement.
sonar.ping_cm() gets me the Ultra Sound distance reading (NewPing library) in cm.

So the code is supposed to measure the distance X times … get the Mean value from those X measurements, compare each individual measurement with the mean value → kick out those individual measurements that are 10% higher or lower than the mean value and calculate a new mean value.

I am using an ESP8266 12F.

The Code is from this site: Link
Here’s the Code:

//-------------------------------------------------------//
int getAverage(float X)
{
    int i, values[70],total_count = 0, new_count = 0,total_distance =0,distance,  average, temp;
    
    for(i=0; i<X; i++){
        distance = sonar.ping_cm();
            total_distance += distance;
            values[total_count++]=distance;
            Serial.printf("Value %d...\n", distance);
            delay(100);
        
    }
    
    if(total_count > 0) {
      
        average = total_distance / total_count;
        total_distance = 0;
        Serial.printf("Average %d...\n", average);
        
        for(i=0; i<total_count; i++){
            temp = (average - values[i])*100/average;
            Serial.printf("Analyze %d....%d...\n", values[i],temp);
            if(temp < 10 && temp > -10){
                total_distance += values[i];
                new_count++;
            }
        }

        if(new_count > 0) {
            return total_distance / new_count;
        }
    }
    return 0;
}
//-------------------------------------------------------//
void loop() {
 delay(50);
 distance = getAverage(70);
}

Question:
The Code works until “X” and the array “values” are assigned a value <= 70.
What is the reason for the following output? (not enough memory?)
This is the normal output for everything below 70:

Value 181...
Value 182...
Value 182...
Value 181...
...goes on like this 
Analyze 181....0...
Analyze 182....-1...
Analyze 183....-1...
Analyze 182....-1...

an this is the output as soon as a assign 70 or higher:

Value 182...
Value 181...
Value 182...
Value 182...
Value 181...
...goes on like this 
Analyze 181....0...
Analyze 182....-1...
Analyze 183....-1...
Analyze 182....-1...
Analyze 181....0...
Analyze 181....0...

Soft WDT reset

ctx: cont 
sp: 3ffef690 end: 3ffef900 offset: 01b0

>>>stack>>>
3ffef840:  000000b6 000000b5 000000b6 000000b6  
3ffef850:  40107370 3ffee5dc 00000000 000000b6  
3ffef860:  000000b6 000000b6 000000b6 40202d80  
3ffef870:  00000103 000000b6 000000b7 000000b6  
3ffef880:  000000b5 00000008 ffffffff 40202e5e  
3ffef890:  000000b5 3ffee5ee 00000017 00000012  
3ffef8a0:  402010d2 00000032 00000032 3ffee8d8  
3ffef8b0:  000000b5 00000000 3ffee8d0 40202046  
3ffef8c0:  3fffdad0 00000000 3ffee5ac 402024d4  
3ffef8d0:  3fffdad0 00000000 3ffee8d0 40201fde  
3ffef8e0:  3fffdad0 00000000 3ffee8d0 40203800  
3ffef8f0:  feefeffe feefeffe 3ffee8e0 40100718  
<<<stack<<<
int getAverage(float X)

You want to calculate the average of 3.14159 values?

Passing 140.5643 to a function that writes to the 140th position in a 70 element array is not a good idea.

Sorry I don't get your point:
If I am calling distance = getAverage(60); and the array is set for 60 elements everything works.

is

int getAverage(float X)

the reason the code behaves that way?

int getAverage(float X)
{
    int i, values[70],total_count = 0, new_count = 0,total_distance =0,distance,  average, temp;
    
    for(i=0; i<X; i++){
        distance = sonar.ping_cm();
            total_distance += distance;
            values[total_count++]=distance;
            Serial.printf("Value %d...\n", distance);
            delay(100);
        
    }

Look at this function. Let’s imagine that you call it with X = 71. So it hits that for loop and writes to values[0] then values[1] then values[2] all the way up to values[71]. But values only has 70 places, numbered 0 to 69. values[70] and values[71] don’t exist. When you try to write to them the processor will let you because it is legal code, but it is going to overwrite whatever is in memory right after the array. When you write past the end of an array like that all bets are off and anything can happen from nothing to a total crash of the program.

If you want to allow for numbers larger than 69, then you need to make the values array larger, large enough to hold how many values you plan to put in there.

Since the only use of X in the getAverage() method is as the end condition of the for loop, which uses an integer index, why is X a float?

As I mentioned in my first post I altered X and the array until I get an error.
So when using X = 69 and values[69] --> no problem
when using X = 70 and values[70] --> problem

So when using X = 69 and values[69] --> no problem
when using X = 70 and values[70] --> problem

You are wrong, there. When you write beyond the end of an array, you might get away with it. You might not. That you got away with it for small values does NOT mean "no problem".

Well 69 was legal. That less than 70. values[69] is the last spot in the array. As soon as you use 70 you are writing to the 71st element of a 70 element array and stomping on memory that you don't own.