a question about the size of a char array

Hello guys. Please explain the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream.h>
#include<conio>
#include <vcl.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{

char a[5];int i;

for (i=0;i<10;i++)
scanf("%c",&a[i]);

for (i=0;i<10;i++)
printf("%c\n",a[i]);
getch();
        return 0;
}

and the output for this is:
abcdefghij
a
b
c
d
e
f
g
h
i
j


why no runtime error? i'm out of array size but it works
There is no check that you are writing within the array; the operating system will halt things with a segFault if you write over memory that the operating system thinks you shouldn't be. Clearly, you're writing beyond the array, but the memory immediately following the array is considered to be yours by the operating system so there is no segFault.

To sum up; writing beyond an array is not checked and there is no guarantee that it will cause a segFault.
Thank you very much, Moschops
Topic archived. No new replies allowed.