Linked Questions
48 questions linked to/from Struct padding in C++
2 votes
2 answers
432 views
Why size of derived class having virtual inheritance is coming unexpected? [duplicate]
As the size of empty class is 1 byte but when this empty class is virtually inherited that (on gcc compilers) the answer to sizeof(derived1) is coming out to be 8 byte...,how come this is happening as ...
1 vote
1 answer
332 views
Class and structure data placement C/C++ [duplicate]
As far as I know, class and struct data is placed one variable after another, for example: class Foo { int A; char B; float* C; double* D; }; Foo Object; char* ptr = &Object; &...
0 votes
0 answers
116 views
C++ writes garbage values between data, when writing a struct to a file [duplicate]
I have been debugging this simple c++ program for hours but I cannot figure out why it writes garbage values in to text file. when I use fwrite to write data struct to "users.txt" file it looks like ...
1 vote
0 answers
63 views
Why is getting extra bytes in a convertion from Struct to array bytes? [duplicate]
I have an issue with C++ and VS-2015 with the next structure: struct HEADER { char BRI; char crv_lenght; __int16 crv; char messagetype; }; The size of this structure is 5 bytes, but ...
2 votes
0 answers
62 views
Typical case in padding of structures in c++ [duplicate]
struct s1 { int a; char b; }; struct s2 { char b; }; struct s3 { char a[3]; }; int main() { s1 obj1; s2 obj2; s3 obj3; cout<<sizeof(obj1)<<endl; // ...
126 votes
5 answers
74k views
Memory alignment : how to use alignof / alignas?
I work with shared memory right now. I can't understand alignof and alignas. cppreference is unclear : alignof returns "alignment" but what is "alignment" ? number of bytes to add for the next block ...
61 votes
10 answers
60k views
Memory alignment in C-structs
I'm working on a 32-bit machine, so I suppose that the memory alignment should be 4 bytes. Say I have this struct: typedef struct { unsigned short v1; unsigned short v2; unsigned short v3; ...
54 votes
7 answers
13k views
Why doesn't GCC optimize structs?
Systems demand that certain primitives be aligned to certain points within the memory (ints to bytes that are multiples of 4, shorts to bytes that are multiples of 2, etc.). Of course, these can be ...
45 votes
7 answers
7k views
Who decides the sizeof any datatype or structure (depending on 32 bit or 64 bit)? [duplicate]
Who decides the sizeof any datatype or structure (depending on 32 bit or 64 bit)? The compiler or the processor? For example, sizeof(int) is 4 bytes for a 32 bit system whereas it's 8 bytes for a 64 ...
30 votes
2 answers
53k views
Memory Alignment in C/C++
I was reading Game Coding Complete 4th edition. There was a topic regarding Memory alignment. In the code below the author says that first struct is really slow because it is both not bit-aligned nor ...
13 votes
6 answers
5k views
sizeof variadic template (sum of sizeof of all elements)
Considering the following function : template<typename... List> inline unsigned int myFunction(const List&... list) { return /* SOMETHING */; } What is the most simple thing to put ...
8 votes
2 answers
4k views
Are std::optional members stored contiguously?
I suppose I'm a bit confused as to how exactly optional values are stored. When constructing a class or struct that contains std::optional<T> members, will these members be stored contiguously ...
12 votes
3 answers
3k views
Is it safe to reinterpret_cast an array to a struct containing the array?
Let's say I have an array of doubles, is it safe to reinterpret_cast it to an array of structs each containing 4 doubles, with regards to alignment? Consider this example code: double *edges = ...; // ...
7 votes
3 answers
3k views
sizeof(), alignment in C structs:
Preface: Did my research about struct alignment. Looked at this question, this one and also this one - but still did not find my answer. My Actual Question: Here is a code snippet I created in order ...
4 votes
2 answers
3k views
Size of struct is more than expected [duplicate]
I already read this question: struct padding in c++ and this one Why isn't sizeof for a struct equal to the sum of sizeof of each member? and I know this isn't standardized but still I believe it'...