vendredi 26 octobre 2018

Does adding a static constexpr member change the memory mapping of a struct/class?

Note: This question arised in the context of shared memory between a C++ and C# program.

In C++11, does adding a static constexpr member change anything in term of memory mapping? I would intuitively say that a static constexpr member doesn't occupy any memory, but I suppose I am ignoring some very fundamental aspect, like polymorphism for example...

So, in the following example, are an instance of Dummy and an instance of Dummy2 guaranteed to occupy the same amount of memory?

struct Dummy {
  static constexpr std::size_t kSize = 512;
  char data[kSize];
};


static constexpr std::size_t kSize2 = 512;
struct Dummy2 {
  char data[kSize2];
};

In this test this theory is not disproved, but I am very far from being able to say that this is guaranteed.

int main() {
    std::cout << sizeof(Dummy) << " " << sizeof(Dummy2) << std::endl;
}

512 512

Aucun commentaire:

Enregistrer un commentaire