sizeof a struct with 4 6-bit members [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)
Closed 5 years ago.
Why is this code:
#include <iostream>
struct S {
unsigned char a:6, b:6, c:6, d:6;
};
int main(int argc, char *argv[]) {
std::cout << sizeof(S);
return 0;
}
returning 4? Shouldn't it have a size of 4 x 6 = 24b = 3B? In contrast, this code:
struct S { unsigned char a:4, b:4, c:4, d:4; };
returns a 2, while this one:
struct S { unsigned char a:4, b:4, c:4, d:4, e:4, f:4; };
returns a 3...

From CppReference: Bit Fields:
The following properties of bit fields are implementation-defined:
[...]
Everything about the actual allocation details of bit fields within the class object
For example, on some platforms, bit fields don't straddle bytes, on others they do
So yes, it's implementation-defined (i.e., up to the compiler).
So, even though the compiler calculates the other 2 without padding, it adds padding the first one?
Yes, because in the first case, values would "straddle" byte borders if not padded, while in the other two examples, they don't.
Again, this is implementation-defined and could be handled differently on another platform, a different compiler on the same platform, or even a different version of the same compiler.

Related

C++ : Size of structure? [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 5 years ago.
My c++ code
#include <iostream>
using namespace std ;
struct Node {
int data ;
struct Node *next;
};
int main(){
struct Node *head = NULL;
struct Node *second = NULL;
cout << sizeof(struct Node);
}
output to terminal
16
How is the size 16 ?
Size of int is 4bytes.
How come it's multiplied by 4 ?
Please can anyone give detailed calculation ?
Thanks !
An int is indeed 4 bytes (at least in x86 64 bit machines). A pointer (at least in x86 64 bit machines) is 8 bytes, so in theory the struct could have been 12 bytes. However, it's padded to a multiplication of the native word size (8 bytes) - and the closet round-up of 12 bytes would be 16 bytes.
Structures are packed to the size of "biggest word" used. Eg, if you have such structure:
struct ThreeBytes {
char one;
short two;
};
Its size will be 4 bytes, because field one will be padded to the size of short, i.e. there is unused byte after that filed. If two would be an int, the structure will have size of two ints. This happens if you align your structure to that:
// this structure got size of 4 bytes.
struct ThreeBytes {
char one;
char two;
short three;
};
And this is unaligned one:
// This structure will have size 6
struct ThreeBytes {
char one;
short two;
char three;
};
This is default behavior, there are compiler directives that allow change packing (see #pragma pack, for example, compiler means may be different). Essentially you can set the unit to which fields will be padded or disable padding by setting it to 1. But some platforms do not allow that at all.

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

sizeof(struct) weird output in C++ [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-structs
(10 answers)
Memory overhead for structs with pointers in C [duplicate]
(1 answer)
Closed 9 years ago.
Could somebody please explain the output of this code?
#include <iostream>
using namespace std;
struct Yo{
char sex;
int a;
};
int main() {
Yo c;
cout<<sizeof(c.sex);
cout<<endl<<sizeof(c.a);
cout<<endl<<sizeof(c);
return 0;
}
Output: 1 4 8
How is the size of structure 8?
This is memory alignment.
struct Yo{
char sex; // Takes up 1 byte + 3 for padding
int a; // Takes up 4 bytes
};
The three bytes between sex and a won't be used because the compiler aligns them for better performance. Thus sex ends up using 1 byte of space and the three bytes after the member variable are used as padding to ensure that int a has an address multiple of 4 (it is 4 byte aligned).
Because of structure padding (aka memory alignment). The alignment has to be a power of two (C11 makes this explicit in 6.2.8p4 as stated by #KeithThompson) and because the total size of your structure is 5, the nearest multiple of 4 is 8 so it gives 8, also because the alignment has to be a power of two.
You can use #pragma pack to have the exact size.
#pragma pack(push, 1) /* set alignment to 1 byte boundary */
struct A {
char s;
int a;
};
#pragma pack(pop) // restore to default
Warning: #pragma pack is not in standard C, nor is the assumption that the structure requires 4-byte alignment. As #KeithThompson stated.
"The size of a type must be a multiple of its alignment, but the size needn't be a power of 2. For example, assuming 4-byte int, a structure like struct { int a, b; char d; } will likely have an alignment of 4 and a size of 12. The size is the nearest multiple of the alignment, not the nearest power of 2." - #KeithThompson
Packing is useful to decrease memory, use it when you have a structure full of integers, fixed char lengths, etc. I do not know if it's good to use with pointers but I do not see it useful when using a pointer (e.g. a void * in the struct).
Read more here

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.

what does this mean in c int a:16;? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does 'unsigned temp:3' mean?
please what does this notation mean
int a:16;
I found it is code like this and it does compile.
struct name {
int a:16; }
This is a bitfield.
This particular bitfield doesn't make much sense as you just could use a 16-bit type and you're wasting some space as the bitfield is padded to the size of int.
Usually, you are using it for structures that contain elements of bit size:
struct {
unsigned nibble1 : 4;
unsigned nibble2 : 4;
}
struct name { int a:16; }
It means a is defined as 16-bit memory space. The remaining bits (16 bits) from int can be used to defined another variable, say b, like this:
struct name { int a:16; int b:16; }
So if int is 32-bit (4 bytes), then the memory of one int is divided into two variables a and b.
PS: I'm assuming sizeof(int) = 4 bytes, and 1 byte = 8 bits
struct s
{
int a:1;
int b:2;
int c:7;
};/*size of structure s is 4 bytes and not 4*3=12 bytes since all share the same space provided by int declaration for the first variable.*/
struct s1
{
char a:1;
};/*size of struct s1 is 1byte had it been having any more char _var:_val it would have been the same.*/
It's a bitfield.
I've never seen a 16 bit bitfield; usually that's a short.
http://www.cs.cf.ac.uk/Dave/C/node13.html