Questions tagged [memory-layout]
272 questions
102
votes
3 answers
Struct memory layout in C
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…

eonil
- 83,476
- 81
- 317
- 516
61
votes
7 answers
How do I organize members in a struct to waste the least space on alignment?
[Not a duplicate of Structure padding and packing. That question is about how and when padding occurs. This one is about how to deal with it.]
I have just realized how much memory is wasted as a result of alignment in C++. Consider the following…
user11313931
43
votes
2 answers
Difference between data section and the bss section in C
When checking the disassembly of the object file through the readelf, I see the data and the bss segments contain the same offset address.
The data section will contain the initialized global and static variables. BSS will contain un-initialized…

Angus
- 12,133
- 29
- 96
- 151
36
votes
7 answers
How does pointer comparison work in C? Is it ok to compare pointers that don't point to the same array?
In K&R (The C Programming Language 2nd Edition) chapter 5 I read the following:
First, pointers may be compared under certain circumstances.
If p and q point to members of the same array, then relations like ==, !=, <, >=, etc. work…

Shisui
- 1,051
- 1
- 8
- 23
34
votes
1 answer
Why do virtual memory addresses for linux binaries start at 0x8048000?
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…

aks
- 24,359
- 3
- 32
- 35
31
votes
3 answers
Precise memory layout control in Rust?
As far as I know, the Rust compiler is allowed to pack, reorder, and add padding to each field of a struct. How can I specify the precise memory layout if I need it?
In C#, I have the StructLayout attribute, and in C/C++, I could use various…

eonil
- 83,476
- 81
- 317
- 516
30
votes
2 answers
struct alignment C/C++
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…

Baruch
- 20,590
- 28
- 126
- 201
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
27
votes
2 answers
Guaranteed memory layout for standard layout struct with a single array member of primitive type
Consider the following simple struct:
struct A
{
float data[16];
};
My question is:
Assuming a platform where float is a 32-bit IEEE754 floating point number (if that matters at all), does the C++ standard guarantee the expected memory layout…

lisyarus
- 15,025
- 3
- 43
- 68
25
votes
2 answers
Does a reference have a storage location?
Does a reference have a storage location or is it just an alias for another location? Does this differ by C++ revision or is it consistent with all versions of C++? And if a reference has a storage location, does it then just allow value semantics…

Tarick Welling
- 3,119
- 3
- 19
- 44
22
votes
5 answers
Finding the address range of the data segment
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…

Nick
- 2,821
- 5
- 30
- 35
21
votes
2 answers
[[no_unique_address]] and two member values of the same type
I'm playing around with [[no_unique_address]] in c++20.
In the example on cppreference we have an empty type Empty and type Z
struct Empty {}; // empty class
struct Z {
char c;
[[no_unique_address]] Empty e1, e2;
};
Apparently, the size of…

tom
- 1,520
- 1
- 12
- 26
21
votes
4 answers
Are C-structs with the same members types guaranteed to have the same layout in memory?
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?

Soumya
- 13,677
- 6
- 34
- 49
20
votes
2 answers
Memory layout in Javascript - data-oriented vs object-oriented design
Coming from a background of C/C++, memory layout of objects with regards to reducing cache misses is something that is crucial especially when working on consoles. Data-oriented design is often favored over object-oriented design, in order to help…

smg
- 1,133
- 1
- 11
- 22
19
votes
3 answers
Does C++ guarantee identical binary layout for "trivial" structs with a single trivial member?
We have some strictly typed integer types in our project:
struct FooIdentifier {
int raw_id; // the only data member
// ... more shenanigans, but it stays a "trivial" type.
};
struct BarIdentifier {
int raw_id; // the only data member
//…

Martin Ba
- 37,187
- 33
- 183
- 337