Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure of a C++ Object in Memory Vs a Struct

Tags:

c++

struct

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.

like image 251
hhafez Avatar asked Jan 08 '09 00:01

hhafez


People also ask

How are structures stored in memory in C?

Struct members are stored in the order they are declared. (This is required by the C99 standard, as mentioned here earlier.) If necessary, padding is added before each struct member, to ensure correct alignment. Each primitive type T requires an alignment of sizeof(T) bytes.

Is a struct an object in C?

In C# on the other hand, struct is used to create value types while class is for reference types. C has structs and is not object oriented.

How is struct in memory?

Memory allocation:Struct will be always allocated memory in Stack for all value types. Stack is a simple data structure with two operations i.e. Push and Pop . You can push on the end of the stack and pop of the end of stack by holding a pointer to the end of the Stack. All reference types will be stored in heap.

How are structure elements stored in memory?

If we create an object of some structure, then the compiler allocates contiguous memory for the data members of the structure. The size of allocated memory is at least the sum of sizes of all data members. The compiler can use padding and in that case there will be unused space created between two data members.


1 Answers

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.

like image 96
j_random_hacker Avatar answered Oct 09 '22 09:10

j_random_hacker