This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t sizeof for a struct equal to the sum of sizeof of each member?
How is the size of a C++ class determined?
When I check the size of the class with single char variable, it's size is 1 Byte. But if we add an integer, suddenly it goes to 8. Can you please explain Why?
class Char
{
char b;
};
class Int
{
int a;
};
class A
{
int a;
char b;
};
int main()
{
Char Cobj;
cout<<"Char size: "<<sizeof(Cobj)<<endl;
Int Iobj;
cout<<"Int size: "<<sizeof(Iobj)<<endl;
A aobj;
cout<<"A size: "<<sizeof(aobj)<<endl;
return 0;
}
The output is:
Char size: 1
Int size: 4
A size: 8
Because of padding - 3 dummy bytes will be added after A::b.
This is done to properly align A in, say, an array - the first member of A is an int, which has to have a specific alignment (4 or 8 bytes probably). So if you have
A arrayOfA[10];
the objects themselves have to be aligned to 4 or 8 bytes.
Related
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 sizeof show 4 bytes for a char? [duplicate]
(1 answer)
Closed last year.
Why size of operator is showing 12 bytes instead of 9 for the below code in c++.
As int occupies 4 bytes, float occupies 4 bytes and character occupies 1 byte, then sizeof should give 9 bytes as output instead of 12.
Object stores memory for data members only not member functions (methods).
#include <iostream>
using namespace std;
class Sample{
public :
int i;
float a;
char ch;
public:
Sample(int j, float b, char dh){
i=j; a=b; ch=dh;
}
};
int main(){
Sample s1(10,3.14f, 'A');
Sample s2(20,6.28f,'B');
cout<<sizeof(s1)<<endl;
cout<<sizeof(s2)<<endl;
return 0;
}
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 6 years ago.
I compiled this code:
// Example program
#include <iostream>
using namespace std;
struct A
{
char a;
};
struct B
{
char b;
int a;
};
struct C
{
int * a;
unsigned char b;
};
int main()
{
cout<< "size of Strcut A:\t"<< sizeof(A)<<endl;
cout<< "size of Strcut B:\t"<< sizeof(B)<<endl;
cout<< "size of Strcut C:\t"<< sizeof(C)<<endl;
cout<< "size of int* : \t"<< sizeof(int*)<<endl;
return 0;
}
And I got this result:
size of Strcut A: 1
size of Strcut B: 8
size of Strcut C: 16
size of int* : 8
now I want to ask why the size of Strcut B is not 5? why the size of Struct C is not 9?
when the memory is importent in Embedded system how I should save memory in another platforms like ARM?
Can I say to the compiler it's 5 bytes or 9 bytes?
http://cpp.sh/2rv6b
Alignment. The members of your datastructures (and their total sizes) are padded with empty space between in order to speed up access and reduce redundant reads that would be necessary when a larger type spans a boundary.
The compiler decides to add some extra padding bits to allign your struct.
It's much faster to work with power of 8 data then spending time to extract them from memory.
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 7 years ago.
I made this minimal working example of a larger piece of code I have.
The problem is, that sizeof prints for the first call 16, which is the correct size, and for the 2nd call only 8. The wierd thing is, it always prints 8, independent of the size of the struct, whether there's only one value or 10 values in it.
struct test_struct
{
int32_t val1;
int32_t val2;
int32_t val3;
int32_t val4;
};
unsigned char * StructToChar(test_struct structy)
{
unsigned char returnval[sizeof(structy)];
memcpy(returnval, &structy, sizeof(structy));
return returnval;
}
int main()
{
test_struct sendstruct = {};
unsigned char *test_array = StructToChar(sendstruct);
unsigned char returnval[sizeof(sendstruct)];
memcpy(returnval, &sendstruct, sizeof(sendstruct));
printf("%d\n", sizeof(returnval));
printf("%d\n", sizeof(test_array));
return 0;
}
sizeof(test_array) is sizeof(unsigned char *) so size of a pointer.
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 ran the following code.
#include <iostream>
using namespace std;
class Base
{
char c;
public:
virtual ~Base()
{
}
};
int main()
{
cout << sizeof(Base) << endl;
return 0;
}
1) The size is 4 (for vptr) + 1(for char). But the result is 8. Why is it so ?
2) I replaced char with a int variable, still the output is 8. Can someone explain me what has caused this issue?
It's down to padding. The compiler has packed your class to a multiple of 4 bytes.
This question already has answers here:
Weird Pointer Address for Individual Struct Data Member
(2 answers)
Closed 8 years ago.
2 questions about the below code:
1 Why is the size of struct b 12 bytes (line 2 of output)? I can understand why a is 12 bytes (to align k on the 4 byte boundary), however shouldnt b be 9 bytes and not 12?
2 Why is using the & operator to get the address of the char members not displaying a valid address? (middle output in the last 2 lines)
#include<iostream>
using namespace std;
struct a
{
int i;
char j;
int k;
};
struct b
{
int i;
int k;
char j;
};
int main()
{
a s1;
b s2;
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
cout<<sizeof(int)<<endl;
cout<<sizeof(char)<<endl;
cout<<&s1.i<<'\t'<<&s1.j<<'\t'<<&s1.k<<endl;
cout<<&s2.i<<'\t'<<&s2.j<<'\t'<<&s2.k<<endl;
}
Output:
12
12
4
1
0x28ff14 - 0x28ff1c
0x28ff08 4A 0x28ff0c
If b were 9 bytes large and you had an array
b foo[2];
then foo[1].i would not be aligned to four-byte boundaries.
As for the char members, their address is of type char *, and std::ostream has an operator<< for those that interprets them as a C-style string. If you want to see their address, static_cast<void*>(&s1.j) is your friend.
EDIT: Full disclosure of egg on my face: This originally stated that some compilers defined minimum struct alignment sizes; upon investigation, this turns out to be untrue. Well, it could be true, since the size of struct types is not specified in the standard to that extent, but common compilers don't appear to do it. Sorry about that.
Why is the size of struct b 12 bytes (line 2 of output)?
It has int fields, and so has the same alignment requirement as int - four bytes, in your case. Three padding bytes are added at the end, so the size is a multiple of four, to give the required alignment.
This applies even at the end of the structure, so that the next element of an array is properly aligned.
Why is using the & operator to get the address of the char members not displaying a valid address?
Because << interprets a pointer to a character type as a pointer to a C-style string, and prints memory contents until it finds a zero value. To print the address, cast to a non-character pointer type:
cout << (void*)&j;