Memory is being shared in C++?
<p>Can someone explain to me how data is being utilized since I was messing around with the following code...:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

typedef struct MyStruct {
public:
    void print() {
        printf("MyStruct.print():\n\ta: %i\n\tb: %i\n\n", a, b);
    }
    void store() {
        a = 2;
        b = 3;
    }
private:
    int a, b;
};

typedef struct MyStruct2 {
public:
    void print() {
        printf("MyStruct2.print():\na: %i\nb: %i\n\n", a, b);
    }
    void store() {
        a = 1024;
        b = 3077;
    }
private:
    int a, b;
};

int main() {
    void *ptr = malloc(sizeof(MyStruct)); // sizeof(MyStruct) == sizeof(MyStruct2)
MyStruct* pstruct = (MyStruct*)ptr;

pstruct-&gt;store();
pstruct-&gt;print();

MyStruct2* pstruct2 = (MyStruct2*)ptr;

pstruct2-&gt;store();
pstruct-&gt;print();

return 0;
}
</code></pre>

<p>and I got the following results:</p>

<pre><code>MyStruct.print():
        a: 2
        b: 3

MyStruct.print():
        a: 1024
        b: 3077
</code></pre>

<p>As you can see I didn't allocate any more memory for the pstruct2, yet I was able to access it. Can anyone explain to me, or at least give me a reference/tutorial to something that is close to this that explains it.</p>
