Re: problem in structure
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Wed, 06 Dec 2006 10:38:44 -0500
RAKHE wrote:
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:ThisIsASpamTrap@xxxxxxxxx>
.
- References:
- problem in structure
- From: RAKHE
- problem in structure
- Prev by Date: Re: Few Questions (HW questions already answered by me)
- Next by Date: Re: Doubt in memcpy() and memset()
- Previous by thread: Re: problem in structure
- Next by thread: regarding nested enums
- Index(es):
Relevant Pages
|