memory-layout

Alignment of bitfields inside unions

∥☆過路亽.° 提交于 2019-12-10 16:49:17
问题 I'm a bit puzzled by how the following code gets layed out in memory: struct Thing { union { unsigned value:24; uint8_t bytes[3]; }; Thing(int v) :value(v) {} void foo() { printf("Thing %p value=%d !\n", this, value); } } __attribute__((__packed__)); On gcc 3.3, 4.3 or 4.6 on Linux (without any special options I can think of - only "-Wall -g" on 4.6), the size of the structure is always 4: $ pahole ./union struct Thing { union { unsigned int value; /* 4 */ unsigned char bytes[3]; /* 3 */ }; [

Why is this Rust enum not smaller?

余生颓废 提交于 2019-12-10 15:57:11
问题 Consider this silly enum: enum Number { Rational { numerator: i32, denominator: std::num::NonZeroU32, }, FixedPoint { whole: i16, fractional: u16, }, } The data in the Rational variant takes up 8 bytes, and the data in the FixedPoint variant takes up 4 bytes. The Rational variant has a field which must be nonzero, so i would hope that the enum layout rules would use that as a discriminator, with zero indicating the presence of the FixedPoint variant. However, this: fn main() { println!(

Compile-time re-arrangement of data members?

扶醉桌前 提交于 2019-12-10 13:25:14
问题 I was wondering about a possible way to make memory layout of a class to be more effective in templated code. As far as I know, Standard mandates data members of a class to be laid out in memory on order of their declaration. There might be possible padding done by the compiler to align the data members adding unnecessarily to the size of the class. The idea is to re-arrange data members declarations at compile time to minimize such padding. I did some searching, but couldn't find any info

Why does virtual inheritance need a vtable even if no virtual functions are involved?

我们两清 提交于 2019-12-10 12:48:01
问题 I read this question: C++ Virtual class inheritance object size issue, and was wondering why virtual inheritance results in an additional vtable pointer in the class. I found an article here: https://en.wikipedia.org/wiki/Virtual_inheritance which tells us: However this offset can in the general case only be known at runtime,... I don't get what is runtime-related here. The complete class inheritance hierarchy is already known at compile time. I understand virtual functions and the use of a

Why I can see the several same segments in the /proc/pid/maps output?

你离开我真会死。 提交于 2019-12-10 12:22:25
问题 Test is on the 32 bit Linux The code is as below: int foo(int a, int b) { int c = a + b; return c; } int main() { int e = 0; int d = foo(1, 2); printf("%d\n", d); scanf("%d", &e); return 0; } and when I use cat /proc/pid/maps to see the memory layout, it seems that I can see three text segment for my code and the library. ubuntu% cat /proc/2191/maps 08048000-08049000 r-xp 00000000 08:01 1467306 /home/shuai/work/asm/test1 08049000-0804a000 r--p 00000000 08:01 1467306 /home/shuai/work/asm/test1

how to get the size of the padding at the end of a class

感情迁移 提交于 2019-12-10 11:44:24
问题 I have a class A like this: struct V { virtual void f() = 0; }; struct A { int data; }; struct B : public A , public V { void f() override {} }; MSVC gives me sizeof(A) == 4 and sizeof(B) == 16 on a 64 bit build instead of 12 ( sizeof(void*) + sizeof(A) ) - so there is a 4 byte padding. Is there a way to get that padding? perhaps with some trait? The reason I need this is to do an assert like this: static_assert(sizeof(B) == sizeof(A) + std::is_polymorphic<camera>::value * sizeof(void*));

how to get the size of the padding at the end of a class

倖福魔咒の 提交于 2019-12-08 03:02:26
I have a class A like this: struct V { virtual void f() = 0; }; struct A { int data; }; struct B : public A , public V { void f() override {} }; MSVC gives me sizeof(A) == 4 and sizeof(B) == 16 on a 64 bit build instead of 12 ( sizeof(void*) + sizeof(A) ) - so there is a 4 byte padding. Is there a way to get that padding? perhaps with some trait? The reason I need this is to do an assert like this: static_assert(sizeof(B) == sizeof(A) + std::is_polymorphic<camera>::value * sizeof(void*)); Meaning that I want to ensure that all data is in the base class, but B should be able to be polymorphic

C++: Memory layout of classes using inheritance

谁说胖子不能爱 提交于 2019-12-07 15:54:03
问题 I know how data will be packed is not specified by the standard. I was just trying to get an idea about the memory layout of classes ( esp. how dynamic_cast<void*> guarantees to return a pointer to the start of the most derived class). I could not think of any explanation about the output of the following code: struct A{ int a;}; struct B{ int b;}; struct C: public A, public B { int c;}; struct D:public C {int d;}; int main(){ D* ob=new D; A* a = ob; B* b = ob; C* c = ob; } Printing the

Why is vptr stored as the first entry in the memory of a class with virtual functions?

雨燕双飞 提交于 2019-12-07 08:10:54
问题 For some compilers, if a class has virtual functions then its vptr can be accessed with the address of the first byte of its object. For instance, class Base{ public: virtual void f(){cout<<"f()"<<endl;}; virtual void g(){cout<<"g()"<<endl;}; virtual void h(){cout<<"h()"<<endl;}; }; int main() { Base b; cout<<"Address of vtbl:"<<(int *)(&b)<<endl; return 0; } I know that it is dependent on different compiler behaviors. Since there is the case where vptr is stored as the very first entry, what

What is the StructLayoutAttribute effect on properties in C#?

筅森魡賤 提交于 2019-12-07 04:18:50
问题 I'm defining structs to be received and sent over a communication channel between different devices and different programming languages. For that I explicitly define their layout in memory using StructLayoutAttribute (sequential, pack=1 - in case it matters). From the documentation, I know it works when the struct contains only fields. It seems to also work for 'simple' properties (with empty get; set;). However I don't know if it's always the case. So my question regarding