| Author |
Message |
junw2000@gmail.com *nix forums addict
Joined: 24 Feb 2006
Posts: 92
|
Posted: Wed Jul 19, 2006 3:56 pm Post subject:
sizeof()
|
|
|
For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof(A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?
Thanks.
Jack |
|
| Back to top |
|
 |
Jakob Bieling *nix forums Guru Wannabe
Joined: 20 Feb 2005
Posts: 227
|
Posted: Wed Jul 19, 2006 3:59 pm Post subject:
Re: sizeof()
|
|
|
junw2000@gmail.com wrote:
| Quote: | For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof(A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?
|
What do you expect? The compiler is free to add padding bytes
between the members of the struct. So if you expected less, the rest is
probably padding.
hth
--
jb
(reply address in rot13, unscramble first) |
|
| Back to top |
|
 |
Scott McPhillips [MVP] *nix forums beginner
Joined: 25 Feb 2005
Posts: 32
|
Posted: Wed Jul 19, 2006 4:00 pm Post subject:
Re: sizeof()
|
|
|
junw2000@gmail.com wrote:
| Quote: | For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof(A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?
Thanks.
Jack
|
Compilers are free to insert unused space in a struct to optimize
hardware access to the struct members. In some machines, for example,
it is not possible to directly access a char unless it is aligned on a 4
or 8 byte boundary.
--
Scott McPhillips [VC++ MVP] |
|
| Back to top |
|
 |
Kai-Uwe Bux *nix forums Guru
Joined: 06 May 2005
Posts: 627
|
Posted: Wed Jul 19, 2006 4:34 pm Post subject:
Re: sizeof()
|
|
|
junw2000@gmail.com wrote:
| Quote: | For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof(A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?
|
You probably know (or have read in the other replies) that there can be
padding within a struct. So, I will assume that you are just puzzled as to
why there could be padding at the *end* of a struct (behind the char).
I would conjecture that the compiler does this, so that when you allocate an
array
A* a = new A [12];
the elements of this array will be aligned properly. Very likely, int or
double needs to start on a word boundary on your implementation.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
Rolf Magnus *nix forums Guru
Joined: 21 Feb 2005
Posts: 1236
|
Posted: Wed Jul 19, 2006 5:05 pm Post subject:
Re: sizeof()
|
|
|
junw2000@gmail.com wrote:
| Quote: | For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof(A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?
|
Why not? |
|
| Back to top |
|
 |
Michiel.Salters@tomtom.co *nix forums Guru Wannabe
Joined: 24 Nov 2005
Posts: 185
|
Posted: Thu Jul 20, 2006 12:52 pm Post subject:
Re: sizeof()
|
|
|
Scott McPhillips [MVP] wrote:
| Quote: | Compilers are free to insert unused space in a struct to optimize
hardware access to the struct members. In some machines, for example,
it is not possible to directly access a char unless it is aligned on a 4
or 8 byte boundary.
|
You just happened to pick the one type for which it isn't a problem.
Basically, an alignment requirement of A is never bigger than
sizeof(A),
because else an array of A is impossible. E.g. in a char[20], you can't
have every char aligned at a 4-byte border. &element[1]=1+&element[0].
Now, sizeof(short) might be 4, in which case that can have an alignment
restriction, but sizeof(char) is always 1.
HTH,
Michiel Salters |
|
| Back to top |
|
 |
kendricktamis *nix forums beginner
Joined: 03 Mar 2010
Posts: 2
|
Posted: Thu Mar 04, 2010 7:08 am Post subject:
Re: sizeof()
|
|
|
| Quote: | For the structure below:
struct A{
int a;
double b;
char c;
};
struct A sa;
printf("sizeof(A): %d\n", sizeof(sa));
The output is
sizeof(A): 16
Why it is 16?
|
It display the size of structure.
sizeof (char) = 1
sizeof (double) = 8
sizeof (int) = 4
Sizeof (struct) = number of variable. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|