Linked Questions
12 questions linked to/from Why doesn't GCC optimize structs?
111 votes
11 answers
26k views
Why can't C compilers rearrange struct members to eliminate alignment padding? [duplicate]
Possible Duplicate: Why doesn't GCC optimize structs? Why doesn't C++ make the structure tighter? Consider the following example on a 32 bit x86 machine: Due to alignment constraints, ...
64 votes
7 answers
11k views
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 ...
72 votes
4 answers
101k views
Struct padding in C++
If I have a struct in C++, is there no way to safely read/write it to a file that is cross-platform/compiler compatible? Because if I understand correctly, every compiler 'pads' differently based on ...
49 votes
6 answers
16k views
Do class/struct members always get created in memory in the order they were declared?
This is a question that was sparked by Rob Walker's answer here. Suppose I declare a class/struct like so: struct { char A; int B; char C; int D; }; Is it safe to assume that these ...
73 votes
3 answers
5k views
std::tuple sizeof, is it a missed optimization?
I've checked all major compilers, and sizeof(std::tuple<int, char, int, char>) is 16 for all of them. Presumably they just put elements in order into the tuple, so some space is wasted because ...
30 votes
4 answers
7k views
Is there a GCC keyword to allow structure-reordering?
I know why GCC doesn't re-order members of a structure by default, but I seldom write code that relies on the order of the structure, so is there some way I can flag my structures to be automaticly ...
16 votes
2 answers
10k views
Can a C++ compiler re-order elements in a struct
Can a C++ compiler (specifically g++) re-order the internal elements of a struct? I'm seeing some strange behaviour where I have a structure that contains something like the following: Struct ...
9 votes
6 answers
4k views
How are structs laid out in memory in C++?
Is the way C++ structs are laid out set by the standard, or at least common across compilers? I have a struct where one of its members needs to be aligned on 16 byte boundaries, and this would be ...
5 votes
2 answers
1k views
Structure Packing. Is there a automatic way to do it?
Question: Is there an automatic way to do structure packing? Background: Structure packing is very useful to reduce the memory cost of certain fundamental data. Basically it is the trick to achieve ...
0 votes
3 answers
130 views
Reducing the size of returned/passed data structures in C
I am taking the operating system class in mit online, I completed the first assignement http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-828-operating-system-engineering-fall-...
1 vote
0 answers
607 views
Does the compiler break struct into variables/register in optimization?
When the compiler (gcc, or intel c++) optimizes a for loop with an index that is a member of a struct, can the compiler break the struct into individual variables/register? For example, when one ...
0 votes
1 answer
150 views
Will gcc remove unused class field, in the final executable file?
For example, will gcc/g++(version 9.4, or 7.3) remove unused c and pc in the output file, because this piece of code access only C::i and C::j, but not C::c andC::pc. #include <iostream> ...