memory-layout

Get the size (in bytes) of an object on the heap

落花浮王杯 提交于 2019-12-29 04:20:28
问题 I'm aware you can use MemoryLayout<T>.size to get the size of a type T . For example: MemoryLayout<Int32>.size // 4 However, for class instances (objects), MemoryLayout<T>.size returns the size of the reference to the object (8 bytes on 64 bit machines), not the size of the actual objects on the heap. class ClassA { // Objects should be at least 8 bytes let x: Int64 = 0 } class ClassB {// Objects should be at least 16 bytes let x: Int64 = 0 let y: Int64 = 0 } MemoryLayout<ClassA>.size // 8

Provide AoS access to SoA

风流意气都作罢 提交于 2019-12-25 08:31:24
问题 I have data laid out in memory in a Structure of Arrays (SoA) or Sturcture of Pointers (SoP) form, and have a way to access that data as though it were laid out in Array of Structure (AoS) form -- code given below. However, I am not too happy about use of struct AoS_4_SoP -- although this struct appears to use templates, it is not really generic since, for example, foo and bar are hard-coded inside it. Two questions/requests: 1) For read-write performance, is AoS access provided as good as

Where in the C++ Standard is the memory layout of objects documented?

拈花ヽ惹草 提交于 2019-12-23 12:55:29
问题 This is a big question, so I'm asking for a reference rather than a booklet-sized answer. I'm going through Stroustrup's Tour of C++, and it seems like the way objects are laid out is memory is fundamental to the design of many C++ features, e.g. PODs vs aggregates vs classes with virtual members. Unfortunately, the Tour itself doesn't cover this subject in detail, and skimming the ToCs of some standard references such as C++ Primer 5ed and TCPPPL 4ed doesn't show whether or where they cover

Where in the C++ Standard is the memory layout of objects documented?

风格不统一 提交于 2019-12-23 12:54:40
问题 This is a big question, so I'm asking for a reference rather than a booklet-sized answer. I'm going through Stroustrup's Tour of C++, and it seems like the way objects are laid out is memory is fundamental to the design of many C++ features, e.g. PODs vs aggregates vs classes with virtual members. Unfortunately, the Tour itself doesn't cover this subject in detail, and skimming the ToCs of some standard references such as C++ Primer 5ed and TCPPPL 4ed doesn't show whether or where they cover

Why is there a top_offset in VTT implemented by gcc?

不想你离开。 提交于 2019-12-23 06:02:53
问题 Here is a detailed description of VTT in the top-voted answer.But the answer does not explain why is there a top-offset in the VTT. From my point of view,when we down_cast a base pointer to derived pointer, the compiler already knows the offset needed to be adjusted in compile time (when there is no virtual derivation) ,so there is no need to store a top_offset in situation below: class A { public: int a; }; class B { public: int b; virtual void w(); }; class C : public A, public B { public:

Why is there a top_offset in VTT implemented by gcc?

青春壹個敷衍的年華 提交于 2019-12-23 06:02:03
问题 Here is a detailed description of VTT in the top-voted answer.But the answer does not explain why is there a top-offset in the VTT. From my point of view,when we down_cast a base pointer to derived pointer, the compiler already knows the offset needed to be adjusted in compile time (when there is no virtual derivation) ,so there is no need to store a top_offset in situation below: class A { public: int a; }; class B { public: int b; virtual void w(); }; class C : public A, public B { public:

Virtual inheritance and empty vtable in base class

梦想的初衷 提交于 2019-12-21 06:09:30
问题 There is this code: #include <iostream> class Base { int x; }; class Derived : virtual public Base { int y; }; int main() { std::cout << sizeof(Derived) << std::endl; // prints 12 return 0; } I have read that when some class is virtually inherited then there is created empty vtable for class Derived, so memory layout is as follows: Derived::ptr to empty vtable Derived::y Base::x and it is 12 bytes. The question is - what is purpose of this empty vtable if there are not any virtual methods and

Why class size increases when int64_t changes to int32_t

♀尐吖头ヾ 提交于 2019-12-21 03:12:27
问题 In my first example I have two bitfields using int64_t . When I compile and get the size of the class I get 8. class Test { int64_t first : 40; int64_t second : 24; }; int main() { std::cout << sizeof(Test); // 8 } But when I change the second bitfeild to be a int32_t the size of the class doubles to 16: class Test { int64_t first : 40; int32_t second : 24; }; int main() { std::cout << sizeof(Test); // 16 } This happens on both GCC 5.3.0 and MSVC 2015. But why? 回答1: In your first example

Does a reference have a storage location?

一个人想着一个人 提交于 2019-12-18 18:40:56
问题 Does a reference have a storage location or is it just an alias for another location? Does this differ by C++ revision or is it consistent with all versions of C++? And if a reference has a storage location, does it then just allow value semantics on a pointer like type? How would a reference work when you use it as such: struct aStruct{ int aVariable; aClass& aReferencetoaClass; }; Does it take up space or is it an alias? 回答1: The latest C++20 spec(§ 9.2.3.3) and at least since the C++ 2005

C++ Memory layout of inheritance

偶尔善良 提交于 2019-12-14 03:53:27
问题 If I have two classes, one inheriting from the other, and the child class only containing functions, will the memory layout be the same for both classes? e.g. class Base { int a,b,c; }; class Derived: public Base { // only functions. }; I've read that the compiler can not reorder data members, and I do not require multiple-inheritance on the Derived class. Is there any situation where the memory layout will not be the same? (Multiple inheritance may be needed for the Base class) 回答1: Both