I have this code in C which takes in bunch of char
s
#include<stdio.h>
# define NEWLINE '
'
int main()
{
char c;
char str[6];
int i = 0;
while( ((c = getchar()) != NEWLINE))
{
str[i] = c;
++i;
printf("%d
", i);
}
return 0;
}
Input is: testtesttest
Output:
1
2
3
4
5
6
7
8
117
118
119
120
My questions are:
Why don't I get an out of bounds (segmentation fault) exception although I clearly exceed the capacity of the array?
Why do the numbers in the output suddenly jump to very big numbers?
I tried this in C++ and got the same behavior. Could anyone please explain what is the reason for this?
question from:
https://stackoverflow.com/questions/65929445/unlimited-space-when-array-is-declared-in-heap 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…