I'm trying to understand when any why a segmentation fault is generated. I wrote a small program which writes to an invalid random address.
I do not see a segmentation fault with this program -
int main (void)
{
int c = 6;
*(&c + 1000) = 5;
printf ("0x%llx - %d\n", (unsigned long)&c, c);
return 0;
}
which outputs:
$ gcc segmentationFault.c
$ ./a.out
0x7ffc2f709b5c - 6
$
But, I get a seg fault with below code -
int main (void)
{
int c = 6;
*(&c + 3000) = 5;
printf ("0x%llx - %d\n", (unsigned long)&c, c);
return 0;
}
which produces:
$ gcc segmentationFault.c
$ ./a.out
Segmentation fault (core dumped)
$
Any explanation please?