Unions confusion with sizeof [duplicate] - c++

This question already has answers here:
Why is my union's size bigger than I expected?
(5 answers)
Closed 2 years ago.
I have the following union :
union Variant{
char txt[10];
double floating_point;
};
The confusion comes when I try to print sizeof(Variant) , which outputs 16 bytes.
Why doesn't it output 10 bytes instead of 16 ? if union takes up as much memory as its largest member, it should do max(sizeof(char[10]), sizeof(double)) no ?
Where does the 6 bytes comes from anyway, if in my system a double takes 8 bytes ?

Because of the alignment on the 8 bytes boundary, since the size of the union's largest member double is probably 8 bytes on your machine. If you had an int instead of double, the size would probably be 12.
The size of your char array is 10 bytes, because it has ten elements. Which is greater than your largest member double size, which is 8. So, the next possible alignment is 2 * 8 which is 16.
If your array had 8 elements or less, the size of the union would be 8, etc.

Why doesn't it output 10 bytes instead of 16 ? if union takes up as much mamory as its largest member, it should do max(sizeof(char[10]), sizeof(double)) no ?
The size of a structure or a class is a multiple of the alignment of a member with the largest alignment requirement. Here, that member is double, whose alignment is 8, i.e. alignof(double) == 8 and alignof(Variant) == alignof(double).

Related

size of class in case of composition [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Memory Alignment in C/C++
(1 answer)
Closed 2 years ago.
class Test2
{
char pq;
long double qr;
};
class Test
{
double c;
int a;
char b;
Test2 z;
};
sizeof(Test2)=32
sizeof(Test)=48
Why sizeof(Test) is 48 on a 64-bit operating system?
It is all about memory alignment.
alignment uses to ensure types don't slip from one memory page to another. the compiler ensures the start address of a type is divided by its alignment.
For example: if a type has an alignment of 2 it can't start on an even address if it has an alignment of 4, the start address the last number must be 0,4,8 or c (hexadecimal). and so on, for alignment of 16, the start address (hex again) is always with 0 at the end.
Each type may have a different alignment.
You can use the alignof operator to tell each type's alignment.
You can use the old offsetof to tell what is the starting address of each member in your class.
It's correct ,
The size of class Test2 is 32 because of the max size variable you have is long double
char 1 byte
long double is 16 byte
so memory allocation is like
-----------------------------------------------------------
char 1 byte |.....padding 15 bytes | long double 16 bytes |
-----------------------------------------------------------
total 32
for class Test the largest size variable is of 8 bytes
so memory allocation is
-------------------------------------------------------------------------------------
double 8 byte |4 byte int then 1 byte char | padding 3 bytes | 32 byte Test2 object
-------------------------------------------------------------------------------------
total 48

uint32_t changes from 4 bytes to 6 bytes in linux [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 3 years ago.
i have this struct that is in theory 14 bytes
typedef struct ActionArgument{
ManagedObjectId managed_object;
uint32_t scope1;
OIDType action_type;
uint16_t length; } ActionArgument;
the struct ManagedObjectId is 6 bytes , scope is "suposedly" 4 bytes , OIDType is a uint16_t ( 2 bytes ) so is length.
But the problem is when i print the size of scope i get 4 bytes which is right , but the size of struct ActionArgument becomes 16.
I tried to correct this by spliting scope to 2 uint16_t variables (scope1 and scope2) and it worked .
But i am still intrigued why the size of a uint23_t is 4 but when i put it in a struct it becomes 6? can some one explain to me ?
I am using kalilinux 4.14.0-kali3-amd64
thanks.
The variables are aligned in memory, so there is a padding added between ManagedObjectId and scope1. One easy way to avoid the padding is to change the struct member order.

Size of struct is more than expected [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 7 years ago.
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's a legit question.
Why is the size of this struct 16 on a x64 system?
struct foo { char* b; char a;};
The effective size would be 8 + 1 = 9, but I know there's padding involved. Anyway I thought a would only be padded to reach the size of an int, i.e. with other 3 bytes giving a total of 12 bytes.
Is there any reason why the specific compiler (gcc) thought it should have 16 bytes as a size?
Wild guess: is it possible that the biggest type (e.g. double or in this case x64 pointer) will dictate the padding to use?
Likely the compiler is aligning the struct on an 8-byte word boundary to improve access speed. A struct size of 9 is probably going to slow down the CPU quite a bit with unaligned accesses (plus the stack pointer should never be on an odd address). A size of 12 (3 padding bytes), would work, but some operations, like the FPU operations, prefer an alignment of 8 or 16 bytes.
It is because of memory alignment. By default memory is not aligned on one bye order and this happens. Memory is allocated on 4-byte chunks on 32bit systems.
You can change this behavior by setting __attribute__((packed, aligned(x))) when you define your structure. By this memory is allocated on x-byte chunks.

Why padding in structures is required? [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Structure padding and packing
(11 answers)
Closed 7 years ago.
We know that the correct order of declaring variables in structures changes the size of a structure also because of padding. I have seen the reference here.
Suppose a structure is:
struct s {
char b; //1 for char
char c; //1 for char + 2 for padding
int a; //4 for int
}my_struct;
So the size of the my_struct is 8, but without padding it could be 6 which is less than 8.
So my question is: Why padding is done in structures? What is the necessity of this concept?
Because without padding the structure is of lesser size. My question is not related to when padding take place, it is more concerned about the concept of padding.
Many computer architectures have optimized memory access in an alignment different than 1 byte (usually, 1 word = 4 bytes). Aligned accesses is generally faster than unaligned ones (and sometimes, it is impossible to access unaligned data). For this reason, compilers pack the struct members in an optimally aligned fashion. In your example, it looks like the alignment is of size 2 bytes, thus b is at offset 0, c is at offset 2 and a is at offset 4, totaling 8 bytes (assuming a is 4 bytes long). Or, according to the inline comments, in your example, it looks like the optimal alignment is equal to the data size. Thus, b and c are adjacent because the size is 1 and so is the alignment, but a is 4 bytes long, and hence needs a 4-byte alignment.
At the bottom line - this is all very architecture dependent.

Why does sizeof show 4 bytes for a char? [duplicate]

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Why does a struct consisting of a char, short, and char (in that order), when compiled in C++ with 4-byte packing enabled, come to a 6-byte struct?
(3 answers)
Closed 9 years ago.
So I am currently a student and am having a programming couse.
Today we had about the use of sizeof in different classes(if it had 1 int or 2 int´s and so on)
One part of the example I found weird was this:
class TwoIntAndACharClass
{
public:
int x_;
int y_;
char z_;
};
and the part to test it
TwoIntAndACharClass o3b;
cout << "Sizeof TwoIntAndACharClass = " << sizeof(o3b) << "\n";
So in the program i could see a class with 1 char took 1 byte. So when I saw this I thought I would see 9 bytes and not 12
So first I thought that it was weird but after some time I came to the conclusion that it might save some kind of blocks of 4 bytes.
To be 100% sure that this was true I then tried adding a new variable into the class(a double variable of 8 bytes) and the total size increased from 12 bytes to now 24 bytes. That ment that the char now had to be 8 bytes long so my last theory failed.
My last theory was that it would take the biggest already declared variable and use the size of that for the char variable _z , as this worked with both long long int(8 bytes) and a double(also 8 bytes)
So my question is, is my last theory true - or is it something different making the char take more memory then needed? (my teacher did say that each compiler could handle this differently but I have tried it on microsoft visual studio and a friend tried it on another compiler with the same results, but is that really true, is it the way the compiler handle this?)
Sorry for my poor english.
sizeof gives you the size of the struct, not the sum of the sizes of its members. Due to alignment requirements (ints generally like to be aligned on natural boundaries; 4 bytes on most platforms), the compiler will pad the struct to 12 bytes, so that when you have multiple instances of the struct adjacent to each other (e.g. in an array), they stay correctly aligned.
Most compilers have some custom extension to control padding, e.g. #pragma pack in Microsoft's compiler.