Differences in memory structure in structures

I have the following structure in C ++

struct A { int a; double b; float c; } 

Is there a difference in the memory structure between this structure and the one with the added function,

 struct B { int a; double b; float c; void foo(); } B::foo() { //do stuff } 
+5
source share
4 answers

The C ++ standard ensures that the memory layouts of the C structure and the C ++ class (or the structure is the same) will be identical, provided that the C ++ class / structure meets the POD criteria ("Plain Old Data"), so that mean pod?

A class or structure is a POD if:

All data members are publicly available and the POD itself or the base types (but not referential or pointer-type-members) or arrays of such

  • It has no custom constructors, assignment operators, or destructors.
  • It does not have virtual functions
  • It does not have base classes

So yes, in your case, the memory layout is the same.

Source: C ++ object structure in Vs a Struct memory

+8
source

Since A and B are standard layouts 1 and their common initial sequence consists of each non-static data element 2, they are compatible with layouts.

Is there a difference in memory structure between this structure and a function with an added function

The standard describes only the semantics of an abstract machine, so there is no guarantee that an object of type A will be represented in memory as an object of type B , but types compatible with layouts tend to be.


1) [class]/7

A standard layout class is a class that:

  • does not have non-static data members such as a non-standard class layout (or an array of such types) or a link,
  • does not have virtual functions (10.3) and there are no virtual base classes (10.1),
  • has the same access control (section 11) for all non-static data members,
  • does not have base classes of non-standard layout, or does not have non-static data members in the derived class and no more than one base class with non-static data members, or does not have base classes with non-static data members and
  • does not have base classes of the same type as the first non-static data element.

2) [class.mem]/21 and [class.mem]/22

Two types of standard layout structures (Section 9) are compatible with layouts if they have the same number of non-static data elements and the corresponding non-static data members (in the order of declaration) have types compatible with the layout (3.9).

+4
source

Formally depends on your compiler, but the compiler, in which it declares a non-virtual member function, changes the layout of the class, will be border sabotage. You need such stability to ensure the interoperability on which shared objects rely on each platform.

+2
source

Yes and no...

In your particular case, no. A structure is nothing more than a data container, and a function is located elsewhere. When the function is called, the pointer to the structure is passed as an additional, implicit first parameter, which is displayed as the this pointer inside the function.

Matter changes, however, if you add a virtual function. Although the C ++ standard does not provide for it, vtables are the defacto standard, and the class will receive a pointer to vtable as the first but invisible member. You can try this by printing the size of the object before and after adding the virtual function.

On the other hand, if the class is virtual due to inheritance from another class that already has virtual functions, the memory layout will no longer be changed, since there is already a pointer to the included table vtable (although it will point to a different location for instances of the subclass) .

+1
source

Source: https://habr.com/ru/post/1276001/


All Articles