Re: [Hampshire] [Slightly OT] Arduino coding help - pointers…

Top Page
Author: Hugo Mills
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] [Slightly OT] Arduino coding help - pointers tostructs

Reply to this message
gpg: failed to create temporary file '/var/lib/lurker/.#lk0x56d9b100.hantslug.org.uk.15454': Permission denied
gpg: keyblock resource '/var/lib/lurker/pubring.gpg': Permission denied
gpg: Signature made Tue Aug 3 15:28:26 2010 BST
gpg: using DSA key 20ACB3BE515C238D
gpg: Can't check signature: No public key
On Tue, Aug 03, 2010 at 02:44:42PM +0100, Chris Liddell wrote:
> On 03/08/2010 14:30, Hugo Mills wrote:
> > On Tue, Aug 03, 2010 at 02:19:32PM +0100, Chris Liddell wrote:
> >> Declarations like your code don't declare variables comprising that
> >> number of bits, they still are still the size of the base type (in this
> >> case unsigned int). All the bit length option does (if supported by the
> >> compiler) is restrict the maximum value of the field (and sometimes not
> >> even that - many compilers ignore it completely).
> >>
> >> So your structure is not 16 bits long, it's 192 bits long.
> >
> >    Not true, at least with gcc 4.4.4:

> >
> > hrm@shades:~$ cat <<EOF >foo.c
> >> #include <stdio.h>
> >>
> >> struct received_data {
> >>     unsigned int start:1;
> >>     unsigned int address:4;
> >>     unsigned int command:5;
> >>     unsigned int flags:4;
> >>     unsigned int padding:1;
> >>     unsigned int parity:1;
> >> };

> >>
> >> int main(int argc, char* argv[])
> >> {
> >> printf("%d\n", sizeof(struct received_data));
> >> return 0;
> >> }
> >> EOF
> > hrm@shades:~$ gcc foo.c
> > hrm@shades:~$ ./a.out
> > 4
>
> Ah, that's interesting. It *definitely* used to use the base type.


Not for as long as I've been writing C, which is >mumblety< years,
now. :) A lot of the bit-packed struct specification is
implementation-defined anyway, so maybe I've just never met a compiler
that does it that way.

> Mind you, four bytes is rather strange, since that doesn't seem to fit
> with the structure element alignment - I'd have thought six bytes would
> be the minimum size it could be. I *thought* gcc at least defaulted to
> processor word alignment.


Four bytes is what I'd expect, as "int" is still 4 bytes, even on
my 64-bit machine, and (IIRC) that's what unit bit-packed structs are
allocated in. gcc won't allow bitfields to overflow a 4-byte boundary,
it seems, as the above structure takes 12 bytes (when it's less than
64 bits, and so could fit into 8 bytes):

struct foo {
    int x:23;
    int y:23;
    int z:15;
};


"Processor word alignment" is rather a vague concept to handle on
x86/amd64, due to 30 years of history. It's rather clearer on, say,
Alpha, where non-aligned accesses cause a hardware exception, or early
ARM, where they cost more CPU cycles to do. :)

I'm not sure how you arrive at the value of 6 bytes (unless that's
a typo for 8 bytes, assuming that it's a 64-bit machine).

Hugo.

-- 
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
        --- Great oxymorons of the world, no. 2: Common Sense ---