Hi,
I am trying to write a program where because of overwriting an array beyond its limit, the next stack memory gets modified.
int
main(void)
{
char str[5];
unsigned int a =10000;
unsigned int ipaddr2 = 3774939393;
unsigned int ipaddr1[2] = {3774939393, 5678};
memcpy(&str, &ipaddr2, sizeof(int));
printf("%d\n", a);
a = a + 1;
memcpy(&str, &ipaddr1, 2 * sizeof(int));
printf("%d\n", a);
}
I was expecting that "memcpy(&str, &ipaddr1, 2 * sizeof(int))" writes 8 bytes and therefor corrupts "int a". However the second printf shows correct value. Why it is not crashing or why not showing corrupt value for a?
./a.out
10000
10001
Thanks
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
Why not? str[5] takes 5 bytes. And 'a' comes after that. So via memcpy if I write 8 bytes of data on str then that should overwrite 'a' value also. right?