Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #9738

Re: Structure with unsigned chars and internal alignment

From "Scott Fluhrer" <sfluhrer@ix.netcom.com>
Newsgroups comp.lang.c
References <j3jmrn$cae$1@nnrp.ngi.it>
Subject Re: Structure with unsigned chars and internal alignment
Message-ID <1314744437.862280@rcdn-nntpcache-3> (permalink)
Organization cisco.com
Date 2011-08-30 18:46 -0400

Show all headers | View raw


"pozz" <pozzugno@gmail.com> wrote in message 
news:j3jmrn$cae$1@nnrp.ngi.it...
>I have a struct composed by two arrays of unsigned char.
>
> struct myStruct {
>   unsigned char field1[2];
>   unsigned char field2[30];
> };
>
> Is myStruct *always* 32 bytes long?  Is field2 *always* starting after two 
> bytes the pointer to myStruct (i.e., no padding is allowed between field1 
> and field2)?

Not necessarily; the Standard allows the compiler to add padding between 
field1 and field2, and also after field2.

On the other hand, I've never seen a compiler that would insert padding 
between field1 and field2 in this case.  It's unlikely that a compiler would 
insert padding after field2 (because 32 is such a round number); however, if 
you later change field2 to be (say) 31 bytes (because you've got a new 
record type that's a bit bigger), I wouldn't be shocked if there do exist 
compilers that would pad it out to (say) a multiple of 4 bytes.

>
> In this case, in my application I'd like to read the size of myStruct 
> (between 3 and 32) from a file.  field1 will be always 2-bytes long, 
> field2 will be the size of myStruct minus 2 bytes of field1 (in the case 
> no padding is present between field1 and field2).
>
> I could allocate field2 array dynamically, but I haven't malloc/free on my 
> embedded platform.  So I decided to statically allocate the biggest size 
> (32 bytes, 2 bytes for field1 and 30 bytes for field2).
>
> If the real size of myStruct, read from the configuration file, is 
> struct_size, how can I deduce the size of field2?  Actually I use the 
> following formula:
>
>   field2_size = struct_size - 2
>
> but I don't like it.  It would be wrong if I'll decide to change the size 
> of field1 member,

Modification of the data format would imply a change in the program.  On the 
other hand, if you expect that such a change is even slightly possible, I'd 
suggest that you replace the 2 with a meaningful constant name (say, 
SIZE_FIELD1)

> and it would be wrong if padding is present between the two fields.  Maybe 
> the following is better?
>
>   field2_size = struct_size - offsetof(struct myStruct, field2)
>
> Do you have other suggestions?

If you're looking for something that makes less assumption on the compiler, 
I suppose you could do:

typedef unsigned char myArray[ 32 ];
#define field2_offset 2 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Structure with unsigned chars and internal alignment pozz <pozzugno@gmail.com> - 2011-08-31 00:05 +0200
  Re: Structure with unsigned chars and internal alignment "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2011-08-30 18:46 -0400
    Re: Structure with unsigned chars and internal alignment pozz <pozzugno@gmail.com> - 2011-08-31 08:53 +0200
      Re: Structure with unsigned chars and internal alignment James Kuyper <jameskuyper@verizon.net> - 2011-08-31 07:42 -0400
    Re: Structure with unsigned chars and internal alignment lawrence.jones@siemens.com - 2011-08-31 12:56 -0400
  Re: Structure with unsigned chars and internal alignment Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-30 21:39 -0400
    Re: Structure with unsigned chars and internal alignment pozz <pozzugno@gmail.com> - 2011-08-31 09:07 +0200
      Re: Structure with unsigned chars and internal alignment Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-31 21:26 -0400
        Re: Structure with unsigned chars and internal alignment pozz <pozzugno@gmail.com> - 2011-09-01 07:59 +0200
          Re: Structure with unsigned chars and internal alignment "BartC" <bc@freeuk.com> - 2011-09-01 10:34 +0100
            Re: Structure with unsigned chars and internal alignment pozz <pozzugno@gmail.com> - 2011-09-01 13:07 +0200
            Re: Structure with unsigned chars and internal alignment Keith Thompson <kst-u@mib.org> - 2011-09-01 08:34 -0700
          Re: Structure with unsigned chars and internal alignment Keith Thompson <kst-u@mib.org> - 2011-09-01 08:46 -0700
    Re: Structure with unsigned chars and internal alignment NicStevens <nic.stevens@gmail.com> - 2011-09-12 12:09 -0700
      Re: Structure with unsigned chars and internal alignment "J. J. Farrell" <jjf@bcs.org.uk> - 2011-09-12 20:30 +0100
      Re: Structure with unsigned chars and internal alignment James Kuyper <jameskuyper@verizon.net> - 2011-09-12 15:32 -0400
        Re: Structure with unsigned chars and internal alignment NicStevens <nic.stevens@gmail.com> - 2011-09-14 13:28 -0700
  Re: Structure with unsigned chars and internal alignment "BartC" <bc@freeuk.com> - 2011-08-31 11:50 +0100
  Re: Structure with unsigned chars and internal alignment David Resnick <lndresnick@gmail.com> - 2011-08-31 06:31 -0700

csiph-web