Hopefully the following will clarify for the Questioner how the stack based variables (int a and char b[1]) appear in memory, why the same addresses are printed and the effect of the buffer overflow from the coding error contained in the code here:
#include <stdio.h>
int main()
{
int a;
char b[1];
a = 0;
printf("The memory location of 0 is: %x\n", &a);
a = 100;
printf("The memory location of 100 is: %x\n", &a);
b[1] = 'c';
printf("The memory location of 'c' is: %x\n", &b[1]);
b[1] = 'd';
printf("The memory location of \"C\" is: %x\n", &b[1]);
return 0;
}
Uninitialized variables before assigning 0 to a -
The first byte highlighted is the 1 byte array b and the second 4 bytes highlighted are the int variable a. Note that the addresses in the watch window agree with the addresses in the memory window.
Now, assign 0 to a -
Note that the changes from the assignment are displayed in red.
Assign 100 to a -
Now for the incorrect code that writes to a memory address b[1] that is outside the bounds of the char array. Assign 'c' to b[1] -
Results of coding error should be evident. The 1 byte char array b is untouched. However, writing outside the bounds of the b array changed the value of the int a variable. Addresses &a and &b[1] point to the same location in memory. The same thing happens when the next statement assigns 'd' to b[1] -
The above results and reproduction of the Questioner's "same address" issue was produced with an x86 debug build with runtime checks disabled.