Date: Sun, 14 Mar 1999 20:34:37 -0500 From: "Mark S. Reichman" <mark@borg.com> Cc: Mark Turpin <mturpin@saturn.spel.com>, Freebsd-questions <freebsd-questions@FreeBSD.ORG> Subject: Re: Programming Question Message-ID: <36EC63AD.C2159EED@borg.com> References: <Pine.BSF.4.05.9903141646550.6443-100000@saturn.spel.com> <36EC34C0.13D218B4@bulinfo.net>
next in thread | previous in thread | raw e-mail | index | archive | help
Check the memory begin addresses of the char and the short struct elements. They are separated by two bytes even though the char is a single byte in length. Sinc struct js is 4 bytes in length the ending address would be 4022328340 + 4 = 4022328344. mark@slugo:~:>cc prog.c -o prog mark@slugo:~:>./prog short 2 int 4 long 4 char 1 float 4 double 8 js 4 Memory address begin for js 4022328340 Memory address begin for js.junk_char 4022328340 Memory address begin for js.junk_short 4022328342 ---- prog.c starts here ------------------------- struct junk_struct { char junk_char; short junk_short; }; main() { struct junk_struct js; printf("short %lu\n", (unsigned long) sizeof(short)); printf("int %lu\n", (unsigned long) sizeof(int)); printf("long %lu\n", (unsigned long) sizeof(long)); printf("char %lu\n", (unsigned long) sizeof(char)); printf("float %lu\n", (unsigned long) sizeof(float)); printf("double %lu\n", (unsigned long) sizeof(double)); printf("js %lu\n", (unsigned long) sizeof(js)); printf("Memory address begin for js %lu\n", (unsigned long) &js); printf("Memory address begin for js.junk_char %lu\n", (unsigned long) &js.junk_char); printf("Memory address begin for js.junk_short %lu\n", (unsigned long) &js.junk_short); return 0; } ---------- cut here ----------- Iani Brankov wrote: > > Mark Turpin wrote: > > > > I feel really silly having to ask this. > > > > Why does my sizeof(mystruct) come out as 4 instead of 3 ( short + char ) ? > > > > struct { > > short shortvariable; > > char charvariable; > > } mystruct; > > > > sizeof(mystruct) == 4 > > Try finding the addresses of the structure members. As I know the compiler > aligns every variable at even address. Maybe that's the reason. > > --iani > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-questions" in the body of the message -- \|/ (@ @) +----------oOO----(_)------------------+ | Mark S. Reichman FreeBSD | | mark@borg.com Got source? | +------------------------oOO-----------+ |__|__| || || ooO Ooo To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?36EC63AD.C2159EED>