memory-layout

Why is this Rust enum not smaller?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 19:40:52
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!("Number = {}", std::mem::size_of::<Number>(),); } Prints: Number = 12 So, the enum gets space for an explicit

Incrementing function pointers

℡╲_俬逩灬. 提交于 2019-12-01 15:35:58
I just learned about function pointers (pointers pointing at the adress where where the machine code of a function is stored). This made me think about machine code and how it is stored in memory. Is the machine code stored consecutively in memory, so that it is possible to "manually" increase the pointer until it points to the following/previous function? Is this, what a debugger does? He lets me "see" where the program counter is pointing in the machine code? Conclusion: one can program with function pointers a primitive debugger? Did I understand this right, or am I way off? BlueRaja -

Why does multiple inheritance increase the size of the object despite the bases being empty?

半腔热情 提交于 2019-12-01 13:54:06
问题 Given this code: #include <iostream> struct A { }; struct B { }; struct C { }; struct E : A { int field; }; struct F : A, B { int field; }; struct G : A, B, C { int field; }; int main() { std::cout << _MSC_VER << std::endl; std::cout << sizeof(E) << std::endl; std::cout << sizeof(F) << std::endl; std::cout << sizeof(G) << std::endl; int o; std::cin >> o; return 0; } I am given the following output: 1900 4 8 8 Why would F and G have sizes of 8 even though their bases are empty? And why would

What about memory layout means that []T cannot be converted to []interface in Go?

依然范特西╮ 提交于 2019-12-01 12:23:22
So I've been reading these two articles and this answer Cannot convert []string to []interface {} says that the memory layout needs to be changed. http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go says that understanding the underlying memory makes answering this question easy, and http://research.swtch.com/interfaces , explains what is going on under the hood. But for the life of me I can't think of a reason, in terms of the implementation of interfaces as to why []T cannot be cast to []interface. So Why? VonC The article " InterfaceSlice " try to detail: A variable with

What about memory layout means that []T cannot be converted to []interface in Go?

大兔子大兔子 提交于 2019-12-01 09:55:28
问题 So I've been reading these two articles and this answer Cannot convert []string to []interface {} says that the memory layout needs to be changed. http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go says that understanding the underlying memory makes answering this question easy, and http://research.swtch.com/interfaces, explains what is going on under the hood. But for the life of me I can't think of a reason, in terms of the implementation of interfaces as to why []T cannot

Type trait to identify primary base class

旧城冷巷雨未停 提交于 2019-12-01 04:13:20
If I have a class Base, with at least one virtual function, and a class Derived which inherits singly from this then (uintptr_t)derived - (uintptr_t)static_cast<Base*>(derived) is guaranteed (by the Itanium ABI) to be zero, even though Derived is not standard layout. However in the general case this is not necessarily true (eg. multiple inheritance). Is it possible to write a trait which can be used to detect if one class is the primary base class of another? Useful sections from the Itanium ABI: http://refspecs.linux-foundation.org/cxxabi-1.83.html Primary base class For a dynamic class, the

understanding size command for data bss segment in C

冷暖自知 提交于 2019-11-30 17:59:34
问题 I'm getting unexpected output from size command. Afaik initialized global and static variables stored in data segment and uninitialized and initialized to 0 global/static variables stored in bss segment. printf("%d",sizeof(int)); gives int size 4. However, bss and data segment is not increasing accordingly to 4. #include <stdio.h> int main() { return 0; } C:\Program Files (x86)\Dev-Cpp\MinGW64\bin>size memory-layout.exe text data bss dec hex filename 10044 2292 2512 14848 3a00 memory-layout

Is kernel space mapped into user space on Linux x86?

妖精的绣舞 提交于 2019-11-30 09:44:41
问题 It seems that on Windows 32 bit, kernel will reserve 1G of virtual memory from the totally 4G user virtual memory space and map some of the kernel space into this 1G space. So my questions are: Is there any similiar situation on 32 bit Linux? If so, how can we see the whole memory layout ? I think cat /proc/pid/map can only see the user space layout of certain process.. Thank you! 回答1: Actually, on 32-bit Windows, without the /3G boot option, the kernel is mapped at the top 2GB of linear

About the memory layout of programs in Linux

∥☆過路亽.° 提交于 2019-11-30 09:20:38
问题 I have some questions about the memory layout of a program in Linux. I know from various sources (I'm reading "Programming from the Ground Up") that each section is loaded into it's own region of memory. The text section loads first at virtual address 0x8048000, the data section is loaded immediately after that, next is the bss section, followed by the heap and the stack. To experiment with the layout I made this program in assembly. First it prints the addresses of some labels and calculates

Mismatch of 'this' address when base class is not polymorphic but derived is

﹥>﹥吖頭↗ 提交于 2019-11-30 09:06:04
There is this code: #include <iostream> class Base { public: Base() { std::cout << "Base: " << this << std::endl; } int x; int y; int z; }; class Derived : Base { public: Derived() { std::cout << "Derived: " << this << std::endl; } void fun(){} }; int main() { Derived d; return 0; } The output: Base: 0xbfdb81d4 Derived: 0xbfdb81d4 However when function 'fun' is changed to virtual in Derived class: virtual void fun(){} // changed in Derived Then address of 'this' is not the same in both constructors: Base: 0xbf93d6a4 Derived: 0xbf93d6a0 The other thing is if class Base is polymorphic, for