problem in structure

R

RAKHE

hi

struct name
{
char n2;
short n1;
long n3;

};

here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc
if any one find the answer could you explain how it is happening
 
K

Kenneth Brody

RAKHE said:
hi

struct name
{
char n2;
short n1;
long n3;

};

here actually sizeof(struct name) is 7;
but why it is printing it as 8 in gcc

Because sizeof(struct name) _is_ 8.
if any one find the answer could you explain how it is happening

It's called "padding" and it's in the FAQ somewhere. I'm sure
someone will post the section shortly.

Basically, your system probably requires (or at least "prefers") to
have shorts on even byte boundaries (and, perhaps, longs on 4-byte
boundaries). Therefore, the compiler adds a byte of padding between
n2 and n1. Use the offsetof() macro to demonstrate this, by printing
the offset of n2, n1, and n3.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

CBFalconer

RAKHE said:
santosh wrote:
.... snip ...

thank u santhosh for ur kind information

I don't believe santoshs initial is 'u', nor that he resides in ur,
which is an ancient city in Mesopotamia. In other words, don't use
childish abbreviations in Usenet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top