Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the memory layout of a class vs. a struct

I come from C programming where the data in a struct is laid out with the top variable first, then the second, third and so on..

I am now programming in C++ and I am using a class instead. I basically want to achieve the same, but I also want get/set methods and also maybe other methods (I also want to try do it in a C++ style and maye learn something new).

Is there a guarantee e.g. that the public variables will be first in memory then the private variable?

like image 276
uniquenamehere Avatar asked Nov 14 '14 22:11

uniquenamehere


People also ask

How is struct stored in memory?

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 between struct members, to ensure that the latter one uses the correct alignment. Each primitive type T requires an alignment of sizeof(T) bytes.

What are the difference between class and structure in terms of memory allocation?

Structures and classes differ in the following particulars: Structures are value types; classes are reference types. A variable of a structure type contains the structure's data, rather than containing a reference to the data as a class type does. Structures use stack allocation; classes use heap allocation.

What is the main difference between a struct and a class?

Difference between Structs and Classes: Struct are value types whereas Classes are reference types. Structs are stored on the stack whereas Classes are stored on the heap. Value types hold their value in memory where they are declared, but a reference type holds a reference to an object in memory.

Does struct use less memory than class?

Answers. "is known that struct objects consume less memory than class objects because struct object does not need an additional memory location to store the memory address of the object created from the new operator."


1 Answers

Is there a guarantee e.g. that the public variables will be first in memory then the private variable?

No, such a guarantee is not made - C++11 standard, [class.mem]/14:

Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11).

So

struct A
{
    int i, j;
    std::string str;

private:

    float f;

protected:

    double d;
};

It is only guaranteed that, for a given object of type A,

  • i has a smaller address than j and
  • j has a smaller address than str

Note that the class-keys struct and class have no difference regarding layout whatsoever: Their only difference are access-rights which only exist at compile-time.


It only says the order, but not that the first variable actually start at the "first address"? Lets assume a class without inheritance.

Yes, but only for standard-layout classes. There is a row of requirements a class must satisfy to be a standard-layout class, one of them being that all members have the same access-control.
Quoting C++14 (the same applies for C++11, but the wording is more indirect), [class.mem]/19:

If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member. Otherwise, its address is the same as the address of its first base classsubobject (if any). [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ]

[class]/7:

A standard-layout class is a class that:

  • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • has no virtual functions (10.3) and no virtual base classes (10.1),
  • has the same access control (Clause 11) for all non-static data members,
  • has no non-standard-layout base classes,
  • either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
  • has no base classes of the same type as the first non-static data member. 110

110) This ensures that two subobjects that have the same class type and that belong to the same most derived object are not allocated at the same address (5.10).

like image 75
Columbo Avatar answered Nov 10 '22 17:11

Columbo