You're [probably] compiling without optimization.
When you loop past the end of array
, you are writing into the location where c
is stored.
So, you're resetting the value of c
to 5
.
So, the UB (undefined behavior) produces an infinite loop and not a segfault.
To cause a segfault, replace:
array[c] = 5;
With (e.g.):
array[c] = 150000;
Also, if that's not enough, increase the number of iterations. Replace the for
loop with (e.g):
for (c = 0; c < 20000; c++)
Here's the complete code that gets a segfault on my system:
#include <stdio.h>
int
main()
{
printf("start
");
printf("Ending
");
int array[5] = { 1, 2, 3, 4, 5 };
int c;
for (c = 0; c < 10000000; c++)
array[c] = 15000;
printf("Done
");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…