linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Smith <sos22@cam.ac.uk>
To: Mohammed Khalid Ansari <khalid@ncst.ernet.in>
Cc: linux c programming mailing list <linux-c-programming@vger.kernel.org>
Subject: Re: (F)structure size
Date: Wed, 5 Mar 2003 09:06:56 +0000	[thread overview]
Message-ID: <20030305090656.GA375@cam.ac.uk> (raw)
In-Reply-To: <Pine.LNX.4.33.0303051227480.30291-100000@soochak.ncst.ernet.in>

[-- Attachment #1: Type: text/plain, Size: 839 bytes --]

> why the sizeof sturcture is alway greater than the collective sizes of all 
> the elements in it.
> 
> eg
> struct node {
> 	int	a;
> 	char	c;
> 	int	b;
> };
It is not, in general, possible to (portably) estimate the size of a
structure just by looking at its fields, as the compiler is free to add
padding if that allows better code.

In particular, most systems can only efficiently access memory
locations if the address of the location is a multiple of its size
(i.e. if its naturally aligned).  This means that if the structure is
to be stored in an array, its size must be a multiple of the size of
the largest single member, so that every element has proper alignment.
In this case, the largest member is an int, and so the whole
structure's size must be a multiple of four.

Steven Smith,
sos22@cam.ac.uk.

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

  parent reply	other threads:[~2003-03-05  9:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-05  7:02 structure size Mohammed Khalid Ansari
2003-03-05  8:27 ` Helmut Djurkin
2003-03-25 10:00   ` strange SIGSEGV Alexi Jordanov
2003-03-05  9:06 ` Steven Smith [this message]
2003-03-12 22:49 ` getlogin(), prints ((NULL)) when printing multiple times in a row J.
2003-03-12 23:14   ` Glynn Clements
2003-03-13  0:18     ` getlogin(), prints ((NULL)) solved.. thnkx.. glen J.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030305090656.GA375@cam.ac.uk \
    --to=sos22@cam.ac.uk \
    --cc=khalid@ncst.ernet.in \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).