output??

63 views
Skip to first unread message

Sandeep

unread,
Aug 6, 2011, 9:02:59 AM8/6/11
to algogeeks_c_problems
struct demo
{
char c;
double d;
int s;
};
what wud be the size of struct object?

saurabh singh

unread,
Aug 6, 2011, 10:35:10 AM8/6/11
to algogeeks_...@googlegroups.com
16 bytes....
suppose starting address is 2000
char c will occupy 2000th position
Now compiler will look for next address thats multiple of 4.This is done to improove access speed.
double starts from 2004-2012.(8 bytes)
s starts from 2012-2016.
Total size=2016-2000=16 bytes
--
Saurabh Singh
B.Tech (Computer Science)
MNNIT ALLAHABAD


SANDEEP CHUGH

unread,
Aug 6, 2011, 10:55:00 AM8/6/11
to algogeeks_...@googlegroups.com
OUTPUT IS 24 ACCORDING TO FOLLOWING

CHAR (1) + 7 BYTE PADDING + DOUBLE (8) +INT(4)+ 4 BYTE PADDING

AND COMPILER WILL LOOK FOR ADDRESS WHICH IS A MULTIPLE OF 8 , NOT 4
BECAUSE MAXIMUM SIZED ELEMENT IS DOUBLE AND IT TAKES 8 BYTES.

saurabh singh

unread,
Aug 6, 2011, 11:02:03 AM8/6/11
to algogeeks_...@googlegroups.com
answer is 16 according to my logic...check with a gcc compliant compiler.

saurabh singh

unread,
Aug 6, 2011, 11:02:39 AM8/6/11
to algogeeks_...@googlegroups.com
would be 8 on 64 bit m/c....4 on a 32  bit m/c

SANDEEP CHUGH

unread,
Aug 6, 2011, 11:06:34 AM8/6/11
to algogeeks_...@googlegroups.com
out put is 24 for 32 bit compiler

and if am using turbo c it wud be 11

saurabh singh

unread,
Aug 6, 2011, 12:18:48 PM8/6/11
to algogeeks_...@googlegroups.com

Tushar

unread,
Aug 6, 2011, 1:52:08 PM8/6/11
to algogeeks_c_problems
@saurabh
i tried the same thing in the morning and got the answer as 16 bytes
but not able to understand why only 4 bytes are reserved for char when
double is also present.
why aren't char and int padded according to double?

shall we pad char to end in multiple of 4 only when any other data
type is present?

saurabh singh

unread,
Aug 6, 2011, 10:37:14 PM8/6/11
to algogeeks_...@googlegroups.com
If I were a compiler I would have tried to pad according to the m/c size rather than the type of members...I havent read much of struct padding but my logic says pad for multiples of 4 for 32 bit addresses and pad for 8 in 64 bit addresses,

Tushar Bindal

unread,
Aug 7, 2011, 3:54:01 AM8/7/11
to algogeeks_...@googlegroups.com
thanks for explaining that
--
Tushar Bindal
Computer Engineering
Delhi College of Engineering
Mob: +919818442705
E-Mail : tushi...@gmail.com
Website: www.jugadengg.com

Reply all
Reply to author
Forward
0 new messages