Stack corruption due to array bound overwrite - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Stack corruption due to array bound overwrite

deathdeath hellPosts: 1Member


I am trying to write a program where because of overwriting an array beyond its limit, the next stack memory gets modified. 

include <stdio.h>


 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?





  • deathdeath hellPosts: 1Member

    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?

Sign In or Register to comment.