struct size not as expected when using bit fields [duplicate] - c++

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Padding in structures in C
(5 answers)
Closed 6 years ago.
With the below code:
struct S {
unsigned char b1 : 3;
int i;
};
I can deduce that char b1 is of 1 byte and using 3 bits of that and int is 4 bytes but when I try to print out the size of S, I get it 8 bytes instead of 5 bytes(1 for char+4 for int) as I expected and this is something I am not understanding by obvious.
Can something explain why size of S is 8 bytes?

Related

Unions confusion with sizeof [duplicate]

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).

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.

Is there an actual 4-bit integer data type in C++11 [duplicate]

This question already has answers here:
Is it possible to create a type in c++ that takes less than one byte of memory?
(5 answers)
Closed 5 years ago.
I require a 4 bit integer in a design for less memory use. In any version of c++ ,c++11 ,c++14 any can be used for the design.
There is no native 4bit datatype. But you could use an 8bit one to hold two 4bit values in the high/low nibble.
no, but you can use:
struct A {
unsigned int value1 : 4;
unsigned int value2 : 4;
};

What does this short C++ code mean? [duplicate]

This question already has answers here:
What does C++ struct syntax "a : b" mean
(5 answers)
":" (colon) in C struct - what does it mean? [duplicate]
(3 answers)
When to use bit-fields in C
(16 answers)
Closed 5 years ago.
This was a code snippet from a test question. The question was what the size of S would be.
struct S
{
char a : 4;
unsigned char b : 3;
signed char : 2;
char c : 1;
char d : 5;
};
What does the ":" do? Is there any difference when it is applied to a signed or unsigned char (or any other data type)? When is this usually used?
It's a bit field, it says that for example char a will only have 4 bits of memory instead of normal 8. Unsigned char b will have only 3 bits of memory. Number of bits limits the range of values it can hold.
Bit Field declares a class data member with explicit size, in bits. Adjacent bit field members may be packed to share and straddle the individual bytes.
http://en.cppreference.com/w/cpp/language/bit_field

Convert uint64_t to 8 byte little endian long [duplicate]

This question already has answers here:
How do I convert a value from host byte order to little endian?
(7 answers)
Closed 6 years ago.
I have a variable called length that is of type uint64_t. I want to convert it to 8 byte little endian long and I'm very confused about these data types.
Can someone guide me on how to make this conversion?
Well, there is Boost.Endian.
#include <boost/endian/conversion.hpp>
#include <iostream>
#include <cstddef>
int main() {
std::uint64_t foo = 1;
std::cout << boost::endian::endian_reverse(foo);
}
Output: 72057594037927936