-3

Given the following, you'll see that x and y are the same size, but y has an additional function. What things are included in sizeof, and what things are not?

struct x
{
    double a;
    double b;
    double c;
    double d;
};
struct y
{
    double a;
    double b;
    double c;
    double d;
    y(double q, double r, double s, double t) : a(q), b(r), c(s), d(t) {};
};
std::cout << sizeof(x)-sizeof(y) <<std::endl;
haccks
  • 104,019
  • 25
  • 176
  • 264
Carbon
  • 3,828
  • 3
  • 24
  • 51
  • `y()` isn't virtual? – EOF Jun 28 '17 at 20:22
  • Data members contribute to the size of a struct, as each struct instance has its own complement of them; ordinary function members do not, as there is only one per struct type, and it is not contained within the struct. –  Jun 28 '17 at 20:22
  • Member functions can be viewed as almost the same as a free function that takes a reference to the class type as the first parameter. – Justin Jun 28 '17 at 20:22
  • Ah, so if there members with points, they don't contribute to the size? – Carbon Jun 28 '17 at 20:22
  • @Justin - like self in python? – Carbon Jun 28 '17 at 20:23
  • 2
    Imagine you had an array of 100 `struct y`'s. Would you need 100 copies of the compiled code for the function? –  Jun 28 '17 at 20:23
  • What did you research before asking here? – Walter Jun 28 '17 at 20:25
  • 1
    @walter: https://stackoverflow.com/questions/3203162/what-does-sizeof-do http://en.cppreference.com/w/cpp/language/sizeof http://en.cppreference.com/w/cpp/language/sizeof... – Carbon Jun 28 '17 at 20:35
  • A very similar question answered quite nicely is right here: https://stackoverflow.com/questions/24504913/where-are-functions-of-an-object-stored-in-memory –  Jun 28 '17 at 20:39

2 Answers2

6

What things are included in sizeof, and what things are not?

sizeof() only includes things that have to be stored in memory as part of each instance of the type. (For a class or structure, it returns the amount of memory that needs to be allocated for an instance.)

Since the function is the same for every instance of struct y, it's not stored as part of the structure -- defining it there just allows the function to be called as a method on the structure. If it were a function pointer, though, the pointer would be part of the structure, and would contribute to its size.

2
5.3.3 Sizeof                                                                                                         [expr.sizeof]

1     The sizeof operator yields the number of bytes in the object representation of its operand.
       The operand is either an expression, which is an unevaluated operand (Clause 5), or a
       parenthesized type-id. The sizeof operator shall not be applied to an expression that has
       function or incomplete type, to an enumeration type whose underlying type is not fixed
       before all its enumerators have been declared, to the parenthesized name of such types, or to
       a glvalue that designates a bit-field. sizeof(char), sizeof(signed char) and
       sizeof(unsigned char) are 1. The result of sizeof applied to any other fundamental type
       (3.9.1) is implementation-defined. [ Note: in particular, sizeof(bool),
       sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.75
       — end note ] [ Note: See 1.7 for the definition of byte and 3.9 for the definition of object
       representation
. — end note ]

2     When applied to a reference or a reference type, the result is the size of the referenced type.
       When applied to a class, the result is the number of bytes in an object of that class
       including any padding required for placing objects of that type in an array. The size of a
       most derived class shall be greater than zero (1.8). The result of applying sizeof to a base
       class subobject is the size of the base class type.76 When applied to an array, the result is
       the total number of bytes in the array. This implies that the size of an array of n elements is n
       times the size of an element.

3     The sizeof operator can be applied to a pointer to a function, but shall not be applied directly
       to a function.

C++ Online Working Draft

In C++, no sane implementation would store a redundant copy the function's machine code within an object instance; not only would it be a massive waste of space, most architectures (that I'm aware of, anyway) don't allow you to mix machine code with data that way, so it doesn't contribute to the size of the object representation.

Community
  • 1
  • 1
John Bode
  • 119,563
  • 19
  • 122
  • 198