object-layout

Under what conditions is it safe to use std::memcpy to copy between objects?

不羁岁月 提交于 2020-01-23 13:03:17
问题 Under what set of conditions is it safe to use std::memcpy to copy from one object to another? For example, what conditions must T , src and dest satisfy for the following to be safe: template <typename T> void copy_bytewise(T& dest, const T& src) { std::memcpy(&dest, &src, sizeof(T)); } The only thing we can assume about src and dest is that they don't overlap 1 . In particular either of src or dest may be a reference to a member or base class. I am interested in answers which refer to the

Memory Layout difference between nested classes and multiple inheritance in C++?

此生再无相见时 提交于 2020-01-20 06:52:42
问题 I am trying to understand how COM specifies the layout of its objects so that a client that wants to use a COM object knows how to do it. I've read that a COM object that implements multiple interfaces can do it it in different ways including using nested classes or multiple inheritance. My understanding is that both techniques would have to produce the same memory layout (conforming to the COM spec) so that a client that wants to use the COM object (for example in C), knows how to do it. So

C++ Can constant class data be optimized out of class by compiler?

回眸只為那壹抹淺笑 提交于 2020-01-14 14:23:13
问题 I know that constant variables outside classes can be optimized directly into function calls by the compiler, but is it legal for the compiler to do the same for constant class variables? If there is a class declared like this: class A { public: const int constVar; //other, modifiable variables A(int val): constVar(val) { //code to initialize modifiable variables } }; and I create an instance of A and call a function like this: A obj(-2); int absoluteVal = std::abs(A.constVar); is the

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

Do data members form a range?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 06:53:58
问题 Can I treat consecutive data members of the same type as a range? For example: struct X { int a, b, c, d, e; }; X x = {42, 13, 97, 11, 31}; std::sort(&x.a, &x.a + 5); // kosher? 回答1: No, this is undefined behaviour. You are treating x.a like the first element of an array, which it isn't. May work on some implementations, may raid your fridge too ;) 回答2: Don't do that. Compiler is free to add paddings between structure members(and at the end). 回答3: If this is really something you want to do,

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

What is in java object header

别等时光非礼了梦想. 提交于 2019-12-17 02:31:36
问题 Could you give me some information on what is exactly stored in object header? I know, that it's probably JVM dependent, but maybe for HotSpot at least? I'm looking for exact description specifically for a first row. I've read several information that I can't verify positively with information I find. Maybe you have a link to OpenJDK wiki that says it all? 回答1: For HotSpot: The object header consists of a mark word and a klass pointer. The mark word has word size ( 4 byte on 32 bit

Why is there internal fragmentation in a Java object even if every field is 4-byte aligned?

☆樱花仙子☆ 提交于 2019-11-30 21:23:16
Intro: I used the JOL (Java Object Layout) tool to analyze the internal and external fragmentation of Java objects for research purpose. While doing so, I stumbled across the following: x@pc:~/Util$ java -jar jol-cli-0.9-full.jar internals sun.reflect.DelegatingClassLoader # WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope # Running 64-bit HotSpot VM. # Using compressed oop with 3-bit shift. # Using compressed klass with 3-bit shift. #

Why is there internal fragmentation in a Java object even if every field is 4-byte aligned?

跟風遠走 提交于 2019-11-30 05:44:34
问题 Intro: I used the JOL (Java Object Layout) tool to analyze the internal and external fragmentation of Java objects for research purpose. While doing so, I stumbled across the following: x@pc:~/Util$ java -jar jol-cli-0.9-full.jar internals sun.reflect.DelegatingClassLoader # WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope # Running