From owner-freebsd-questions Sun Mar 14 17:30:33 1999 Delivered-To: freebsd-questions@freebsd.org Received: from maxcow.borg.com (MaxCow.borg.com [205.217.206.188]) by hub.freebsd.org (Postfix) with ESMTP id 9D08C15C2D for ; Sun, 14 Mar 1999 17:30:21 -0800 (PST) (envelope-from mark@borg.com) Received: from mail.borg.com (mail.borg.com [205.217.206.192]) by maxcow.borg.com (8.9.0/8.8.8) with ESMTP id UAA18504; Sun, 14 Mar 1999 20:30:02 -0500 (EST) Received: from borg.com (ip32a.borg.com [208.3.180.32]) by mail.borg.com (8.8.7/8.7.3) with ESMTP id UAA24124; Sun, 14 Mar 1999 20:29:56 -0500 (EST) Message-ID: <36EC63AD.C2159EED@borg.com> Date: Sun, 14 Mar 1999 20:34:37 -0500 From: "Mark S. Reichman" X-Mailer: Mozilla 4.5 [en] (X11; I; FreeBSD 3.1-STABLE i386) X-Accept-Language: en MIME-Version: 1.0 Cc: Mark Turpin , Freebsd-questions Subject: Re: Programming Question References: <36EC34C0.13D218B4@bulinfo.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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