size of Struct with pointer [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 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.

Related

Why sizeof is printing 12 bytes instead of 9 for object of a class? [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 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;
}

How much size will a class take if a pointer to an instance of it is used inside its definition? [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 2 years ago.
I am trying to implement a linked list. I am curious about how much space will this node structure take. I tried it with sizeof(node) and got 16 bytes but I had expected 12 bytes as int on my system is 4 bytes and a pointer is 8 bytes.
Note that this is pseudocode.
/*a pseudo c++ file to demonstrate linked list*/
node{
int val;
node *p;
};
linkedlist{
...
...
};
I think you have not declared your node correctly. You should declare it as below:
struct node{
int val;
node *p;
};
and to know the size you can do something like below in your main function:
int main()
{
node n;
cout << "size of node: " << sizeof (n) << endl;
}
in my system it shows the below output, As you have one int variable and one pointer in node, so in my system int is the size of 4 and the pointer's size depends on the int size because the size of a pointer is same as int, so 4 + 4 =8 :
size of node: 8

Struct sizes and address of struct members [duplicate]

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;

Why struct size do not match with plain sum of item's sizes? [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 9 years ago.
With C/C++ structs, normally, the final struct's size is the plain sum of the sizes of all its elements, but there are cases which this is not true.
I am looking at the technical answer (it has to be one) of why the following struct's size is not equal to the size of all its members:
#include <iostream>
struct {
int a;
long b;
char c;
} typedef MyStruct;
int main() {
MyStruct sss;
std::cout << "Size of int: " << sizeof(int) << std::endl << "Size of long: " << sizeof(long) << std::endl << "Size of char: " << sizeof(char) << std::endl;
std::cout << "Size of MyStruct: " << sizeof(sss) << std::endl;
return 0;
}
Thas has the following output:
Size of int: 4
Size of long: 8
Size of char: 1
Size of MyStruct: 24
So it can be seen than the size of MyStruct is not 4+8+1 (13) rather, it is actually 24, but why?
Because C allows compilers to add padding between the elements of a struct to make the generated code more performant.
Some hardware allows accessing multi-byte data faster when such data is placed at memory addresses divisible by the size of the data. For example, accessing a 32-bit int may go significantly faster when the int is located at address 0x8004 or 0x8008 than when the same int is located at an address 0x8003 or 0x8006. The standard allows compilers to adjust offsets of struct members to make use of such optimization.
Moreover, there are platforms where accessing a multi-byte value not aligned with the memory space results in an error: for example, reading a 16-bit int from an odd address in 68000 architecture triggers bus error. Compilers know this, and they add unused space to your struct so that accessing multi-byte fields does not trigger an error.

Why char size increases as instance variable [duplicate]

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.