niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
· nixdoc.net · man pages · Linux HOWTOs · FreeBSD Tips · Forums
navigation Forum index » Programming » C++
sizeof()
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
Author Message
junw2000@gmail.com
*nix forums addict


Joined: 24 Feb 2006
Posts: 92

PostPosted: Wed Jul 19, 2006 3:56 pm    Post subject: sizeof() Reply with 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
Back to top
Jakob Bieling
*nix forums Guru Wannabe


Joined: 20 Feb 2005
Posts: 227

PostPosted: Wed Jul 19, 2006 3:59 pm    Post subject: Re: sizeof() Reply with quote

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

PostPosted: Wed Jul 19, 2006 4:00 pm    Post subject: Re: sizeof() Reply with quote

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

PostPosted: Wed Jul 19, 2006 4:34 pm    Post subject: Re: sizeof() Reply with quote

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

PostPosted: Wed Jul 19, 2006 5:05 pm    Post subject: Re: sizeof() Reply with quote

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

PostPosted: Thu Jul 20, 2006 12:52 pm    Post subject: Re: sizeof() Reply with quote

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

PostPosted: Thu Mar 04, 2010 7:08 am    Post subject: Re: sizeof() Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [7 Posts] View previous topic :: View next topic
The time now is Fri Jul 30, 2010 2:49 am | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts sizeof() junw2000@gmail.com C 12 Wed Jul 19, 2006 3:51 pm
No new posts Implementing the sizeof operator abhishek.srivastava78@gma C++ 32 Wed Jul 05, 2006 11:43 am
No new posts MIPSPro compiler sizeof classes philparsonage@hotmail.com apps 2 Wed Jun 28, 2006 2:00 pm
No new posts sizeof pnreddy1976@gmail.com C 90 Sun Jun 18, 2006 11:42 am
No new posts sizeof 'a' returns 4 Laurentiu C 29 Sun Jun 18, 2006 5:58 am

Copyright © 2004-2005 DeniX Solutions SRL   Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum |  Privacy Policy



Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0650s ][ Queries: 14 (0.0344s) ][ GZIP on - Debug on ]