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 102455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:58:00+00:00 2026-05-11T00:58:00+00:00

If I have a class as follows class Example_Class { private: int x; int

  • 0

If I have a class as follows

   class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     }; 

And a struct as follows

struct {    int x;    int y; } example_struct; 

Is the structure in memory of the example_struct simmilar to that in Example_Class

for example if I do the following

struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&foo_struct, &foo_class, sizeof(foo_struct)); 

will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?

The reason I ask is I have a C++ library (don’t want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I’m only interested in the attributes of the object.

I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.

  • 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. 2026-05-11T00:58:01+00:00Added an answer on May 11, 2026 at 12:58 am

    The C++ standard guarantees that memory layouts of a C struct and a C++ class (or struct — same thing) will be identical, provided that the C++ class/struct fits the criteria of being POD (‘Plain Old Data’). So what does POD mean?

    A class or struct is POD if:

    • All data members are public and themselves POD or fundamental types (but not reference or pointer-to-member types), or arrays of such
    • It has no user-defined constructors, assignment operators or destructors
    • It has no virtual functions
    • It has no base classes

    About the only ‘C++-isms’ allowed are non-virtual member functions, static members and member functions.

    Since your class has both a constructor and a destructor, it is formally speaking not of POD type, so the guarantee does not hold. (Although, as others have mentioned, in practice the two layouts are likely to be identical on any compiler that you try, so long as there are no virtual functions).

    See section [26.7] of the C++ FAQ Lite for more details.

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There are two big problems: You can't specify a constructor… May 11, 2026 at 2:35 pm
  • added an answer Do you use any type of IOC container? Unity is… May 11, 2026 at 2:35 pm
  • added an answer The IWin32Window parameter is for specifying the owner of the… May 11, 2026 at 2:35 pm

Related Questions

Sorry if this is generic in nature, but I have a question that maybe
Say I have a type that implements a property with a string type: public
I am trying to export a Ruby framework via XML-RPC. However I am having
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.