Questions tagged [object-layout]
24 questions
63
votes
3 answers
C vs C++ struct alignment
I've been asked in a recent interview about C++ struct fields alignment and theoretized that C and C++ follows the same strategy in struct packing.
Hovewer, it was the wrong assumption. The interviewer said that in general C and C++ are packing…

Minor Threat
- 2,025
- 1
- 18
- 32
52
votes
3 answers
What is in Java object header?
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…

alobodzk
- 1,284
- 2
- 15
- 27
28
votes
4 answers
Why class size increases when int64_t changes to int32_t
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…

xinaiz
- 7,744
- 6
- 34
- 78
16
votes
5 answers
Do data members form a range?
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?

fredoverflow
- 256,549
- 94
- 388
- 662
14
votes
4 answers
Virtual inheritance in C++
I found this in a website while reading about virtual inheritance in c++
When multiple inheritance is used, it is sometimes necessary to use virtual inheritance. A good example for this is the standard iostream class hierarchy:
//Note: this is a…

Vijay
- 65,327
- 90
- 227
- 319
11
votes
3 answers
Is a C++ object layout necessarily statically defined?
More specifically, assuming A is an accessible base class of B, does the following code produce undefined behavior, and is the assertion guarenteed not to fire according to the standard?
void test(B b1, B b2) {
A* a2 = &b2;
auto offset =…

SomeStrangeUser
- 727
- 11
- 16
7
votes
2 answers
Why is there internal fragmentation in a Java object even if every field is 4-byte aligned?
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…

Markus Weninger
- 11,931
- 7
- 64
- 137
4
votes
1 answer
Under what conditions is it safe to use std::memcpy to copy between objects?
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
void copy_bytewise(T& dest, const T& src) {
…

BeeOnRope
- 60,350
- 16
- 207
- 386
4
votes
1 answer
Where in the C++ Standard is the memory layout of objects documented?
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.…

Dun Peal
- 16,679
- 11
- 33
- 46
3
votes
3 answers
Does extra inheritance make any difference on object structure or instantiation?
In the code there are some special classes and there are some normal classes. I want to differentiate them because special classes needed to be given different treatment. All these special classes are base (not child of any other class)
To achieve…

iammilind
- 68,093
- 33
- 169
- 336
3
votes
1 answer
Does the standard require that std::array behave like it does not have trailing padding?
std::array is required by the standard to be well-defined even when the size is zero, so that std::array is well-defined even though int[0] is not allowed (though GCC implements an extension for it).
However, on GCC, Clang, and MSVC,…

Bernard
- 5,209
- 1
- 34
- 64
3
votes
1 answer
java.lang.Integer object layout and it's overhead
I used a tool called JOL (Java Object Layout) which tries to analyze object layout. It comes with a cli and I used it to analyze java.lang.Integer. I see that Integer object is taking 12 extra bytes for overhead. That overhead can be 4 bytes for the…

Node.JS
- 1,042
- 6
- 44
- 114
3
votes
4 answers
Memory Layout difference between nested classes and multiple inheritance in C++?
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…

kybrd_chllgd
- 33
- 3
3
votes
3 answers
C++ Can constant class data be optimized out of class by compiler?
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…

Cpp plus 1
- 990
- 8
- 25
3
votes
1 answer
Java object layout and static fields
The JOL tool gives ability to count object's memory layout.
I've noticed, that static fields do not participate in calculating, for example:
public class Foo {
private static final int i = 1;
private char value;
public Foo(char value)…

Vladimir Kishlaly
- 1,872
- 1
- 16
- 26