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
Related
Someone explain me how does the order of the member declaration inside a class determines the size of that class.
For Example :
class temp
{
public:
int i;
short s;
char c;
};
The size of above class is 8 bytes.
But when the order of the member declaration is changed as below
class temp
{
public:
char c;
int i;
short s;
};
then the size of class is 12 bytes.
How?
The reason behind above behavior is data structure alignment and padding. Basically if you are creating a 4 byte variable e.g. int, it will be aligned to a four byte boundary i.e. it will start from an address in memory, which is multiple of 4. Same applies to other data types. 2 byte short should start from even memory address and so on.
Hence if you have a 1 byte character declared before the int (assume 4 byte here), there will be 3 free bytes left in between. The common term used for them is 'padded'.
Data structure alignment
Another good pictorial explanation
Reason for alignment
Padding allows faster memory access i.e. for cpu, accessing memory areas that are aligned is faster e.g. reading a 4 byte aligned integer might take a single read call where as if an integer is located at a non aligned address range (say address 0x0002 - 0x0006), then it would take two memory reads to get this integer.
One way to force compiler to avoid alignment is (specific to gcc/g++) to use keyword 'packed' with the structure attribute. packed keyword Also the link specifies how to enforce alignment by a specific boundary of your choice (2, 4, 8 etc.) using the aligned keyword.
Best practice
It is always a good idea to structure your class/struct in a way that variables are already aligned with minimum padding. This reduces the size of the class overall plus it reduces the amount of work done by the compiler i.e. no rearrangement of structure. Also one should always access member variables by their names in the code, rather than trying to read a specific byte from structure assuming a value would be located at that byte.
Another useful SO question on performance advantage of alignment
For the sake of completion, following would still have a size of 8 bytes in your scenario (32 bit machine), but it won't get any better since full 8 bytes are now occupied, and there is no padding.
class temp
{
public:
int i;
short s;
char c;
char c2;
};
class temp
{
public:
int i; //size 4 alignment 4
short s; //size 2 alignment 2
char c; //size 1 alignment 1
}; //Size 8 alignment max(4,2,1)=4
temp[i[0-4];s[4-2];c[6-7]]] -> 8
Padding in (7-8)
class temp
{
public:
char c; //size 1 alignment 1
int i; //size 4 alignment 4
short s; //size 2 alignment 2
};//Size 12 alignment max(4,2,1)=4
temp[c[0-1];i[4-8];s[8-10]]] -> 12
Padding in (1-4) and (10-12)
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.
Someone explain me how does the order of the member declaration inside a class determines the size of that class.
For Example :
class temp
{
public:
int i;
short s;
char c;
};
The size of above class is 8 bytes.
But when the order of the member declaration is changed as below
class temp
{
public:
char c;
int i;
short s;
};
then the size of class is 12 bytes.
How?
The reason behind above behavior is data structure alignment and padding. Basically if you are creating a 4 byte variable e.g. int, it will be aligned to a four byte boundary i.e. it will start from an address in memory, which is multiple of 4. Same applies to other data types. 2 byte short should start from even memory address and so on.
Hence if you have a 1 byte character declared before the int (assume 4 byte here), there will be 3 free bytes left in between. The common term used for them is 'padded'.
Data structure alignment
Another good pictorial explanation
Reason for alignment
Padding allows faster memory access i.e. for cpu, accessing memory areas that are aligned is faster e.g. reading a 4 byte aligned integer might take a single read call where as if an integer is located at a non aligned address range (say address 0x0002 - 0x0006), then it would take two memory reads to get this integer.
One way to force compiler to avoid alignment is (specific to gcc/g++) to use keyword 'packed' with the structure attribute. packed keyword Also the link specifies how to enforce alignment by a specific boundary of your choice (2, 4, 8 etc.) using the aligned keyword.
Best practice
It is always a good idea to structure your class/struct in a way that variables are already aligned with minimum padding. This reduces the size of the class overall plus it reduces the amount of work done by the compiler i.e. no rearrangement of structure. Also one should always access member variables by their names in the code, rather than trying to read a specific byte from structure assuming a value would be located at that byte.
Another useful SO question on performance advantage of alignment
For the sake of completion, following would still have a size of 8 bytes in your scenario (32 bit machine), but it won't get any better since full 8 bytes are now occupied, and there is no padding.
class temp
{
public:
int i;
short s;
char c;
char c2;
};
class temp
{
public:
int i; //size 4 alignment 4
short s; //size 2 alignment 2
char c; //size 1 alignment 1
}; //Size 8 alignment max(4,2,1)=4
temp[i[0-4];s[4-2];c[6-7]]] -> 8
Padding in (7-8)
class temp
{
public:
char c; //size 1 alignment 1
int i; //size 4 alignment 4
short s; //size 2 alignment 2
};//Size 12 alignment max(4,2,1)=4
temp[c[0-1];i[4-8];s[8-10]]] -> 12
Padding in (1-4) and (10-12)
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.
Someone explain me how does the order of the member declaration inside a class determines the size of that class.
For Example :
class temp
{
public:
int i;
short s;
char c;
};
The size of above class is 8 bytes.
But when the order of the member declaration is changed as below
class temp
{
public:
char c;
int i;
short s;
};
then the size of class is 12 bytes.
How?
The reason behind above behavior is data structure alignment and padding. Basically if you are creating a 4 byte variable e.g. int, it will be aligned to a four byte boundary i.e. it will start from an address in memory, which is multiple of 4. Same applies to other data types. 2 byte short should start from even memory address and so on.
Hence if you have a 1 byte character declared before the int (assume 4 byte here), there will be 3 free bytes left in between. The common term used for them is 'padded'.
Data structure alignment
Another good pictorial explanation
Reason for alignment
Padding allows faster memory access i.e. for cpu, accessing memory areas that are aligned is faster e.g. reading a 4 byte aligned integer might take a single read call where as if an integer is located at a non aligned address range (say address 0x0002 - 0x0006), then it would take two memory reads to get this integer.
One way to force compiler to avoid alignment is (specific to gcc/g++) to use keyword 'packed' with the structure attribute. packed keyword Also the link specifies how to enforce alignment by a specific boundary of your choice (2, 4, 8 etc.) using the aligned keyword.
Best practice
It is always a good idea to structure your class/struct in a way that variables are already aligned with minimum padding. This reduces the size of the class overall plus it reduces the amount of work done by the compiler i.e. no rearrangement of structure. Also one should always access member variables by their names in the code, rather than trying to read a specific byte from structure assuming a value would be located at that byte.
Another useful SO question on performance advantage of alignment
For the sake of completion, following would still have a size of 8 bytes in your scenario (32 bit machine), but it won't get any better since full 8 bytes are now occupied, and there is no padding.
class temp
{
public:
int i;
short s;
char c;
char c2;
};
class temp
{
public:
int i; //size 4 alignment 4
short s; //size 2 alignment 2
char c; //size 1 alignment 1
}; //Size 8 alignment max(4,2,1)=4
temp[i[0-4];s[4-2];c[6-7]]] -> 8
Padding in (7-8)
class temp
{
public:
char c; //size 1 alignment 1
int i; //size 4 alignment 4
short s; //size 2 alignment 2
};//Size 12 alignment max(4,2,1)=4
temp[c[0-1];i[4-8];s[8-10]]] -> 12
Padding in (1-4) and (10-12)