memory-layout

struct alignment C/C++

醉酒当歌 提交于 2019-11-30 05:43:44
In c/c++ (I am assuming they are the same in this regard), if I have the following: struct S { T a; . . . } s; Is the following guaranteed to be true? (void*)&s == (void*)&s.a; Or in other words, is there any kind of guarantee that there will be no padding before the first member? In C, yes, they're the same address. Simple, and straightforward. In C++, no, they're not the same address. Base classes can (and I would suspect, do) come before all members, and virtual member functions usually add hidden data to the struct somewhere. Even more confusing, a C++ compiler may also rearrange members

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

耗尽温柔 提交于 2019-11-29 13:27:20
问题 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

How many vptr will a object of class(uses single/multiple inheritance) have?

China☆狼群 提交于 2019-11-29 10:35:42
问题 How many vptrs are usually needed for a object whose clas( child ) has single inheritance with a base class which multiple inherits base1 and base2. What is the strategy for identifying how many vptrs a object has provided it has couple of single inheritance and multiple inheritance. Though standard doesn't specify about vptrs but I just want to know how an implementation does virtual function implementation. 回答1: Why do you care? The simple answer is enough , but I guess you want something

Print layout of C++ object with g++ compiler

杀马特。学长 韩版系。学妹 提交于 2019-11-29 06:14:44
Is there a way to print the layout of a C++ object using the g++ compiler or any other means. A simplified example (assuming int takes 4 bytes) class A{ int a; }; class B:public A{ int b; } so the output would be A- 0 4 + a + B- 0 4 8 + A.a + b + It would be useful to understand the layout of objects (in my case virtual machine code). Thanks in advance. Regards, Zaheer Looking at the man pages, -fdump-class-hierarchy maybe? The information you seek is needed by debuggers and is emitted for them when you compile with -g . On ELF/DWARF platforms (such as Linux), you can see what's there by

struct alignment C/C++

女生的网名这么多〃 提交于 2019-11-29 04:53:25
问题 In c/c++ (I am assuming they are the same in this regard), if I have the following: struct S { T a; . . . } s; Is the following guaranteed to be true? (void*)&s == (void*)&s.a; Or in other words, is there any kind of guarantee that there will be no padding before the first member? 回答1: In C, yes, they're the same address. Simple, and straightforward. In C++, no, they're not the same address. Base classes can (and I would suspect, do) come before all members, and virtual member functions

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

戏子无情 提交于 2019-11-28 20:51:01
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 MemoryLayout<ClassB>.size // 8, as well :( How can I get the size of the objects themselves? For those

Why is the ELF entry point 0x8048000 not changeable with the “ld -e” option?

本秂侑毒 提交于 2019-11-28 18:58:07
Following up Why is the ELF execution entry point virtual address of the form 0x80xxxxx and not zero 0x0? and Why do virtual memory addresses for linux binaries start at 0x8048000? , why cannot I make ld use a different entry point than the default with ld -e ? If I do so, I either get a segmentation fault with return code 139, even for addresses close by the default entry point. Why? EDIT: I will make the question more specific: .text .globl _start _start: movl $0x4,%eax # eax = code for 'write' system call movl $1,%ebx # ebx = file descriptor to standard output movl $message,%ecx # ecx =

Why does virtual keyword increase the size of derived a class?

北城以北 提交于 2019-11-28 08:35:13
问题 I have two classes - one base class and one derived from it : class base { int i ; public : virtual ~ base () { } }; class derived : virtual public base { int j ; }; main() { cout << sizeof ( derived ) ; } Here the answer is 16. But if I do instead a non-virtual public inheritance or make the base class non-polymorphic , then I get the answer as 12, i.e. if I do : class base { int i ; public : virtual ~ base () { } }; class derived : public base { int j ; }; main() { cout << sizeof ( derived

Why is the ELF entry point 0x8048000 not changeable with the “ld -e” option?

吃可爱长大的小学妹 提交于 2019-11-27 12:02:30
问题 Following up Why is the ELF execution entry point virtual address of the form 0x80xxxxx and not zero 0x0? and Why do virtual memory addresses for linux binaries start at 0x8048000?, why cannot I make ld use a different entry point than the default with ld -e ? If I do so, I either get a segmentation fault with return code 139, even for addresses close by the default entry point. Why? EDIT: I will make the question more specific: .text .globl _start _start: movl $0x4,%eax # eax = code for

Are C-structs with the same members types guaranteed to have the same layout in memory?

孤街浪徒 提交于 2019-11-27 08:40:44
Essentially, if I have typedef struct { int x; int y; } A; typedef struct { int h; int k; } B; and I have A a , does the C standard guarantee that ((B*)&a)->k is the same as a.y ? Are C-structs with the same members types guaranteed to have the same layout in memory? Almost yes. Close enough for me. From n1516, Section 6.5.2.3, paragraph 6: ... if a union contains several structures that share a common initial sequence ..., and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the