Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8972771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:17:50+00:00 2026-06-15T18:17:50+00:00

Can someone explain to me how data is being utilized since I was messing

  • 0

Can someone explain to me how data is being utilized since I was messing around with the following code…:

#include <stdio.h>
#include <stdlib.h>

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->store();
pstruct->print();

MyStruct2* pstruct2 = (MyStruct2*)ptr;

pstruct2->store();
pstruct->print();

return 0;
}

and I got the following results:

MyStruct.print():
        a: 2
        b: 3

MyStruct.print():
        a: 1024
        b: 3077

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:17:51+00:00Added an answer on June 15, 2026 at 6:17 pm

    Both pstruct and pstruct2 point to the same location in memory (Since you assigned the address stored in ptr to both) and thus the data inserted by the MyStruct::store method was overwritten by the MyStruct2::store method.

    In other words, this is happening because you are explicitly making it happen. If your two classes weren’t identical or if the compiler had produced different memory layouts of them, you would’ve possibly read out garbage data.


    Basically, C++ allows you to write into any dynamically allocated memory as much as you want, happily ignorant and oblivious of the fact that you had previously used this memory for another object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CWM is data modeling UML is object modeling. Can someone explain the difference that
Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
can someone explain why this jquery selector is not working, I've worked around the
Can someone please explain the difference between datum() and data() in D3.js? I see
When using embedded java databases, can someone explain how the database persists data across
Could someone please explain why this is happening; this following is the code I’ve
Can someone explain if JSF can rerender a disabled data table on click of
Can someone explain what this means? int (*data[2])[2];
Can someone explain this JS LINE ? data is an object. var list =
Can someone explain this simple, yet deceiving, anomaly? There are two models, where B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.