int main()
{
int myArray[5];
int i;
for ( i=0; i<10; i++)
{
cout << "Value for myArray[" << i << "]: ";
cin >> myArray[i];
}
for (i = 0; i<10; i++)
cout << i << ": " << myArray[i] << "\n";
return 0;
}
If you notice, i declared array size as 5 and using for loop i am initializing more than 5 values i.e. 10. Practically it should show compilation error. but when i compiled, it gives no error and the output is obtained with 10 values. Why this is happening..??
Why do you think it should show a compilation error? Where is the bad syntax? There is no bad syntax.
There will be a segFault when the operating system thinks you're trying to write over memory it hasn't given you. Clearly, the space immediately after the array is memory that the operating system thinks is yours, so there is no segfault. It is still wrong. You are still trashing data. Do not rely on the operating system to stop you.