#include <iostream>
using namespace std;
struct A
{
int a, b;
};
struct B
{
int a;
};
int main()
{
A * pa = (A *)malloc(sizeof(B));
int c = 5;
pa -> a = 3;
cout << pa -> a << endl;
pa -> b = 0;
cout << pa -> b << endl;
cout << c << endl;
return 0;
}
I run this code with VC++ 2012. It doesn't generate any error message.
I think pa -> b will access memory block outbound. Heap corruption should occur! But actually, nothing happened in both debug and release modes.
But since int c immediately follows A * pa; I think in memory, pa -> b will access int c.
The output of the program is: 3 4 5
Can anyone help explain this?
If I add "free(pa);" at the end of the main: +under debug mode, it will cause HEAP CORRUPTION ERROR. +under release mode, nothing still happens.