From: ninjaboy <n0b0dyn1nj4@gmail.com>
To: Giulio Rossato <giulio.rossato@infocamere.it>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: offsets of fields in a structure
Date: Fri, 19 Oct 2007 16:30:14 +0000 [thread overview]
Message-ID: <a3399010710190930q1a4f7873v3ea8841679b3fb43@mail.gmail.com> (raw)
In-Reply-To: <4718CCE2.9060202@infocamere.it>
2007/10/19, Giulio Rossato <giulio.rossato@infocamere.it>:
> Suppose that I define the following structure
> struct astruct {
> int field1;
> float field2;
> char field3[10];
> };
>
> and declare the variable
> struct astruct r;
>
> The amount of memory specified by the structure is not the sum of the
> storage specified by each of its member types. This vary from one
> machine and C compiler to another. On most compuers, objects of certain
> types may not begin anywhere in memory but are constrained to start at
> certain boundaries. For example, an integer of length 4 bytes may have
> to start at an address divisible by 4, and a real number of length 8 may
> have to start at an address divisible by 8. Thus, in my example, if the
> starting address of r is 200, the integer occupies bytes 200 through
> 203, but the real number cannot start at byte 204, since that location
> is not divisible by 8. Thus the real number must start at byte 208.
> The C compiler associates to each member identifier of a structure an
> offset that specifies how far beyond the start of the structure the
> location of that field is. To calculate the location of a member in a
> structure, the offset of the member identifier is added to the base
> address of the structure variable.
> Now the question.
> I want write a function that receives as parameters a start address and
> a "description of a structure" and returns the offsets of the fields.
> The structure is not known at compile time. The offsets should be
> calculated at runtime and the code should be independent of the machine.
> How should be written this function?
maybe typeof() can help you, and use __attribute__((packed)) if you
want the real size of structure, this is an alignament thingie.
--
noone is alone.
next prev parent reply other threads:[~2007-10-19 16:30 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-19 15:27 offsets of fields in a structure Giulio Rossato
2007-10-19 16:30 ` ninjaboy [this message]
2007-10-19 17:35 ` LaPoint, Adam W
2007-10-19 18:07 ` LaPoint, Adam W
2007-10-19 18:11 ` Glynn Clements
2007-10-19 19:50 ` Jan Smout
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=a3399010710190930q1a4f7873v3ea8841679b3fb43@mail.gmail.com \
--to=n0b0dyn1nj4@gmail.com \
--cc=giulio.rossato@infocamere.it \
--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).