Accessing memory beyond the allocated chunk is an Undefined behavior.
for(i=0;i<5;i++)
{
b[i]=a[i]; //1. does not throw an error
}
In this loop you will end up assigning b[3]=a[3]
and b[4]=a[4]
;
b[3]
and b[4]
are unallocated chunks of memory. So accessing them is an undefined behavior.
There can be unpredictable results due to undefined behavior.
According to standard:
Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).
Edit:
For your doubt about segmentation fault:
Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you". But the reverse is not true i.e you can not say that you'll get a segmentation fault whenever you access an un-allocated memory.
There are systems out there that operate without memory protection, thus you cannot tell whether a piece of memory actually "belongs to you", and thus don't know whether segmentation fault will occur or not, only undefined behavior is assured.