Explanation for an array out of bound output

My brother needed a couple of codes debugged the other day

among them was the following program:

I replaced the inputs at line 13 with lines 3,14 & 15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<math.h>
int values[] ={ 8,10,49,112,98,86,54,37,28,12,6};

main()
{
	int i,n;
	float x[11],f[11],s1=0,s2=0,s3=0,s4=0,m1,m2,m3,m4,b1,b2,TOTAL=0;
	printf("Enter n=");
	scanf("%d",&n);
	printf("Give the observation and corresponding frequency=");
	for(i=1;i<=n;i++){            //this error is left as it is  intentionally
		//scanf("%f%f",&x[i],&f[i]);
		x[i] = i;
		f[i] = values[i];
		printf("\n%f , %f",x[i],f[i]);
		printf("\n%d < %d  = %d",i ,n ,i<=n);
		s1=s1+x[i]*f[i];
		TOTAL=TOTAL+f[i];
	}
	printf("\n_______________________\n");
	m1=s1/TOTAL;
	for(i=1;i<=n;i++){
		s2=s2+pow(x[i]-m1,2)*f[i];
		s3=s3+pow(x[i]-m1,3)*f[i];
		s4=s4+pow(x[i]-m1,4)*f[i];
	}
	m2=s2/TOTAL;
	m3=s3/TOTAL;
	m4=s4/TOTAL;
	b1=(m3*m3)/(m2*m2*m2);
	b2=m4/(m2*m2);
	printf("b1 & b2 are %f & %f respectively",b1,b2);
}


for 'n' up to 10 the program works as expected

Enter n=10
Give the observation and corresponding frequency=
1.000000 , 10.000000
1 < 10  = 1
2.000000 , 49.000000
2 < 10  = 1
3.000000 , 112.000000
3 < 10  = 1
4.000000 , 98.000000
4 < 10  = 1
5.000000 , 86.000000
5 < 10  = 1
6.000000 , 54.000000
6 < 10  = 1
7.000000 , 37.000000
7 < 10  = 1
8.000000 , 28.000000
8 < 10  = 1
9.000000 , 12.000000
9 < 10  = 1
10.000000 , 6.000000
10 < 10  = 1
_______________________
b1 & b2 are 0.370221 & 2.862203 respectivelyPress any key to continue . . .


I initially reasoned for 'n' >= 11 the loop should run 10 times then crash (now i realize that particular outcome is not guaranteed)
but instead it runs exactly 22 times and then crashes

Enter n=11
Give the observation and corresponding frequency=
1.000000 , 10.000000
1 < 11  = 1
2.000000 , 49.000000
2 < 11  = 1
3.000000 , 112.000000
3 < 11  = 1
4.000000 , 98.000000
4 < 11  = 1
5.000000 , 86.000000
5 < 11  = 1
6.000000 , 54.000000
6 < 11  = 1
7.000000 , 37.000000
7 < 11  = 1
8.000000 , 28.000000
8 < 11  = 1
9.000000 , 12.000000
9 < 11  = 1
10.000000 , 6.000000
10 < 11  = 1
11.000000 , 0.000000
11 < 1093664768  = 1
12.000000 , 0.000000
12 < 1093664768  = 1
13.000000 , 0.000000
13 < 1093664768  = 1
14.000000 , 0.000000
14 < 1093664768  = 1
15.000000 , 0.000000
15 < 1093664768  = 1
16.000000 , -1.000000
16 < 1093664768  = 1
17.000000 , 0.000000
17 < 1093664768  = 1
18.000000 , 0.000000
18 < 1093664768  = 1
19.000000 , 0.000000
19 < 1093664768  = 1
20.000000 , 4200752.000000
20 < 1093664768  = 1
21.000000 , 0.000000
21 < 1093664768  = 1
22.000000 , 0.000000
22 < 1093664768  = 1[program crashes]



I noticed the value of 'n' gets modified once the arrays goes out of bound
but how 'n' has been declared before the arrays
in my understanding for 'x[i]' & 'f[i]' for 'i'>10 it should either get a segmentation fault or write into memory location beyond the array bloc

also why is the loop running exactly 22 times?

I figure its a compiler implementation issue but I would like an explanation

initially i tested the code in a win7-geany-gcc version 4.8.1 (rev5, Built by MinGW-W64 project)


when I tested the code in a linux environment ( gcc version 4.2.1 20070831 patched [FreeBSD] )

I got the expected output

Enter n=11
Give the observation and corresponding frequency=
1.000000 , 10.000000
1 < 12  = 1
2.000000 , 49.000000
2 < 12  = 1
3.000000 , 112.000000
3 < 12  = 1
4.000000 , 98.000000
4 < 12  = 1
5.000000 , 86.000000
5 < 12  = 1
6.000000 , 54.000000
6 < 12  = 1
7.000000 , 37.000000
7 < 12  = 1
8.000000 , 28.000000
8 < 12  = 1
9.000000 , 12.000000
9 < 12  = 1
10.000000 , 6.000000
10 < 12  = 1
11.000000 , 0.000000
Segmentation fault: 11 (core dumped)
Last edited on
/bump
for 'n' up to 10 the program works as expected
false

Ask your brother three questions.

Array indexing 101: Given int arr[] = { 7, 3, 8 };
1. How do you access the first element of 'arr'?
2. How do you access the last element of 'arr'?

User input 101: The user requests the 42nd element of 'arr'.
3. What should the program do?
I'm assuming you are pointing to the logical error at the loop indexing in line 12
I pointed that out to him and solved his problem.


I'm concerned how the program behaves WITH the error
closed account (j3Rz8vqX)
In a nice way:

Count from 0, not 1; you've never accessed index 0.

Have fun
@Dput : plz read my above comment
closed account (j3Rz8vqX)
It'll start jumping to the next available memory; or simply everywhere dependent on how you're accessing your data.

If you had an array and accessed an element one index beyond its availability, it may give you the value of the next variable declared, might cycle back to the first, or access something on the stack.

You're likely reaching your limits at the crash index you're reporting.
Behavior may sightly alter if you add processes before and after, but a crash is prone to happen, and faulty data will eventually emerge.

Your program is allowed to access any memory, in ram, your program allocated; if it accesses something forbidden, you will receive a crash.

We can't say for certain what you accessed, just that it eventually access something vital.

Edit:
Different operating systems may handle incorrect memory seek differently; or manage memory differently(storage).
Last edited on
You write to a memory location. You don't know what that memory location is used for. (That depends on the compiler.) Therefore, the result is undefined.

If 'n' is stored at 'f+11' and 'values+11' contains random bits, then f[i] = values[i]; overwrites 'n' with garbage like 1093664768.

At some point the loop will overwrite such location that a crash does happen. (A Debug build by MSVC injects additional range checking code, so that every out of bounds attempt will throw rather than write.)
I didnt no that it
might cycle back to the first


thanks

I'll leave this post open for further opinions
Topic archived. No new replies allowed.