memory-layout

Object layout in case of virtual functions and multiple inheritance

久未见 提交于 2019-11-27 06:50:55
I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved. I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual table, insert a secret pointer to the virtual table in each object and so on). It seemed to me that there was something missing in my explanation. So here are questions (see example below) What is the exact memory layout of the object of class C. Virtual tables entries for class C. Sizes (as returned by sizeof) of object of classes A, B and C. (8, 8

Why do virtual memory addresses for linux binaries start at 0x8048000?

时间秒杀一切 提交于 2019-11-27 03:40:12
Disassembling an ELF binary on a Ubuntu x86 system I couldn't help but notice that the code(.text) section starts from the virtual address 0x8048000 and all lower memory addresses seem to be unused. This seems to be rather wasteful and all Google turns up is either folklore involving STACK_TOP or protection against null-pointer dereferences. The latter case looks like it can be fixed by using a single page instead of leaving a 128MB gap. So my question is this - is there a definitive answer to why the layout has been fixed to these values or is it just an arbitrary choice? From the Linkers and

Finding the address range of the data segment

一个人想着一个人 提交于 2019-11-27 03:35:57
As a programming exercise, I am writing a mark-and-sweep garbage collector in C. I wish to scan the data segment (globals, etc.) for pointers to allocated memory, but I don't know how to get the range of the addresses of this segment. How could I do this? jim mcnamara The bounds for text (program code) and data for linux (and other unixes): #include <stdio.h> #include <stdlib.h> /* these are in no header file, and on some systems they have a _ prepended These symbols have to be typed to keep the compiler happy Also check out brk() and sbrk() for information about heap */ extern char etext,

Virtual tables and memory layout in multiple virtual inheritance

半世苍凉 提交于 2019-11-26 22:41:50
Consider following hierarchy: struct A { int a; A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { int b1; B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10; } }; struct B2 : virtual A { int b2; B2(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+20; } }; struct C : B1, virtual B2 { int c; C() : B1(6),B2(3),A(1){} virtual void f(int i) { cout << i+30; } }; What's the exact memory layout of C instance? How many vptrs it contains, where exactly each of them is placed? Which of virtual tables are shared with virtual table of C?

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

寵の児 提交于 2019-11-26 14:15:37
问题 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 ? 回答1: 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

Object layout in case of virtual functions and multiple inheritance

三世轮回 提交于 2019-11-26 12:57:01
问题 I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved. I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual table, insert a secret pointer to the virtual table in each object and so on). It seemed to me that there was something missing in my explanation. So here are questions (see example below) What is the exact memory layout of the object of class C.

Why do virtual memory addresses for linux binaries start at 0x8048000?

大憨熊 提交于 2019-11-26 10:35:31
问题 Disassembling an ELF binary on a Ubuntu x86 system I couldn\'t help but notice that the code(.text) section starts from the virtual address 0x8048000 and all lower memory addresses seem to be unused. This seems to be rather wasteful and all Google turns up is either folklore involving STACK_TOP or protection against null-pointer dereferences. The latter case looks like it can be fixed by using a single page instead of leaving a 128MB gap. So my question is this - is there a definitive answer

Finding the address range of the data segment

白昼怎懂夜的黑 提交于 2019-11-26 10:34:05
问题 As a programming exercise, I am writing a mark-and-sweep garbage collector in C. I wish to scan the data segment (globals, etc.) for pointers to allocated memory, but I don\'t know how to get the range of the addresses of this segment. How could I do this? 回答1: The bounds for text (program code) and data for linux (and other unixes): #include <stdio.h> #include <stdlib.h> /* these are in no header file, and on some systems they have a _ prepended These symbols have to be typed to keep the

Virtual tables and memory layout in multiple virtual inheritance

限于喜欢 提交于 2019-11-26 08:25:09
问题 Consider following hierarchy: struct A { int a; A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { int b1; B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10; } }; struct B2 : virtual A { int b2; B2(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+20; } }; struct C : B1, virtual B2 { int c; C() : B1(6),B2(3),A(1){} virtual void f(int i) { cout << i+30; } }; What\'s the exact memory layout of C instance? How many vptrs it

Struct memory layout in C

两盒软妹~` 提交于 2019-11-26 00:36:42
问题 I have a C# background. I am very much a newbie to a low-level language like C. In C#, struct \'s memory is laid out by the compiler by default. The compiler can re-order data fields or pad additional bits between fields implicitly. So, I had to specify some special attribute to override this behavior for exact layout. AFAIK, C does not reorder or align memory layout of a struct by default. However, I heard there\'s a little exception that is very hard to find. What is C\'s memory layout