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


Groups > comp.lang.c > #35582

Re: Struct with unaligned fields

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Struct with unaligned fields
Date 2013-08-22 14:57 -0400
Organization Self
Message-ID <52165F15.3000207@verizon.net> (permalink)
References <kv5jvm$7mk$1@dont-email.me>

Show all headers | View raw


On 08/22/2013 02:06 PM, James Harris wrote:
> A file allocation table is an on-disk structure that was designed in the 
> days before people took much care with field alignment. FAT file systems are 
> still prevalent today. I need to write some code to work with such 
> structures so have to deal with fields that will not be aligned.
> 
> To illustrate, the FAT header starts like this:
> 
>   8-byte BS_OEMName
>   2-byte BPB_BytsPerSec
>   1-byte BPB_SecPerClus
>   2-byte BPB_RsvdSecCnt
>   ...
> 
> As you can see, it is impossible to align the last of those fields, 
> BPB_RsvdSecCnt. The same is true of some of the fields that follow. (For 
> anyone who is interested the full list of fields can be seen in a document 
> called fatgen103. Copies are freely available on the internet.)
> 
> I've seen the c faq at http://c-faq.com/struct/padding.html and know that 
> there are no ideal solutions. This post is to ask for suggestions as to how 
> to address this well and portably.

If portable is your goal, structures are NOT the right tool to achieve
it, and that article gives one of main reasons why.

On the other hand, I find it hard to imaging that a tool which works
with FATs would need to be very portable. It could only be useful on
systems which use FATs; I wouldn't expect C implementations targeting
such systems to have a lot of variation in how they deal with alignment
issue.

> I could do something basic and slow but what makes this an interesting 
> challenge is that: 1) many of the fields *will* be naturally aligned so 
> don't need any special access mechanism and 2) some of the machines the code 
> will run on will support unaligned accessed and some will not. It would be 
> good to use the same C source to run on any type of machine and get the 
> compiler to emit the faster code where it can.

The new (C2011) _Alignof() field can be used to check the alignment
requirements of the relevant data types, and you can use it to switch
between faster code that will be slow, or not work at all if the data is
misaligned, or slower code that will work whether or not the data is
misaligned. _Alignof() is, unfortunately, not available for use in #if
conditions, because types, and therefore alignment, do not exist yet
during translation phase 4, when #ifs are evaluated.
It's probably simpler to just write only the slower code; I wouldn't
recommend assuming that it's a lot slower - test the simpler approach
first, and bother with optimization only if you find out that it
actually is significantly slower. A sufficiently smart optimizer might
even convert the slow code into the equivalent of the fast code, on
systems where it is in fact faster.

> Anyone already addressed the same sort of issue?

Yes, I have - by the use of the methods that are probably similar to the
ones you've already dismissed as "slow".

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


Thread

Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-22 19:06 +0100
  Re: Struct with unaligned fields glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-08-22 18:18 +0000
  Re: Struct with unaligned fields James Kuyper <jameskuyper@verizon.net> - 2013-08-22 14:57 -0400
    Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-22 21:05 +0100
      Re: Struct with unaligned fields James Kuyper <jameskuyper@verizon.net> - 2013-08-22 16:45 -0400
      Re: Struct with unaligned fields Keith Thompson <kst-u@mib.org> - 2013-08-22 15:12 -0700
    Re: Struct with unaligned fields glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-08-22 22:22 +0000
    Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-22 22:07 -0500
  Re: Struct with unaligned fields Stephen Sprunk <stephen@sprunk.org> - 2013-08-22 14:13 -0500
    Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-22 22:08 -0500
      Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-23 09:36 +0100
        Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-23 12:48 -0500
    Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-23 09:34 +0100
      Re: Struct with unaligned fields Stephen Sprunk <stephen@sprunk.org> - 2013-08-23 04:47 -0500
        Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-23 15:31 +0100
          Re: Struct with unaligned fields Stephen Sprunk <stephen@sprunk.org> - 2013-08-23 13:31 -0500
            Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-23 20:46 +0100
            Re: Struct with unaligned fields glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-08-23 20:48 +0000
  Re: Struct with unaligned fields Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-22 20:01 +0000
    Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-23 12:54 +0100
      Re: Struct with unaligned fields Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-23 08:17 -0400
      Re: Struct with unaligned fields Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-23 20:00 +0000
      Re: Struct with unaligned fields Tim Rentsch <txr@alumni.caltech.edu> - 2013-08-24 16:50 -0700
  Re: Struct with unaligned fields "BartC" <bc@freeuk.com> - 2013-08-22 21:59 +0100
    Re: Struct with unaligned fields Keith Thompson <kst-u@mib.org> - 2013-08-22 15:02 -0700
    Re: Struct with unaligned fields Tim Rentsch <txr@alumni.caltech.edu> - 2013-08-24 16:42 -0700
  Re: Struct with unaligned fields Siri Cruise <chine.bleu@yahoo.com> - 2013-08-22 14:21 -0700
  Re: Struct with unaligned fields Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-22 17:23 -0400
    Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-23 13:03 +0100
      Re: Struct with unaligned fields Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-23 08:43 -0400
        Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-23 20:20 +0100
          Re: Struct with unaligned fields James Kuyper <jameskuyper@verizon.net> - 2013-08-23 16:15 -0400
          Re: Struct with unaligned fields Ian Collins <ian-news@hotmail.com> - 2013-08-24 11:47 +1200
            Re: Struct with unaligned fields "James Harris" <james.harris.1@gmail.com> - 2013-08-24 14:01 +0100
              Re: Struct with unaligned fields Robert Wessel <robertwessel2@yahoo.com> - 2013-08-24 14:26 -0500
                Re: Struct with unaligned fields Ian Collins <ian-news@hotmail.com> - 2013-08-25 08:55 +1200
                Re: Struct with unaligned fields Robert Wessel <robertwessel2@yahoo.com> - 2013-08-25 04:17 -0500
                Re: Struct with unaligned fields Ian Collins <ian-news@hotmail.com> - 2013-08-25 21:22 +1200
                Re: Struct with unaligned fields Stephen Sprunk <stephen@sprunk.org> - 2013-08-24 16:16 -0500
                Re: Struct with unaligned fields Robert Wessel <robertwessel2@yahoo.com> - 2013-08-25 04:43 -0500
                Re: Struct with unaligned fields David Thompson <dave.thompson2@verizon.net> - 2013-08-28 22:27 -0400
      Re: Struct with unaligned fields "BartC" <bc@freeuk.com> - 2013-08-23 23:55 +0100
  Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-22 22:05 -0500
    Re: Struct with unaligned fields Stephen Sprunk <stephen@sprunk.org> - 2013-08-23 00:29 -0500
      Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-23 12:53 -0500
        Re: Struct with unaligned fields Stephen Sprunk <stephen@sprunk.org> - 2013-08-23 15:15 -0500
          Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-23 22:14 -0500
            Re: Struct with unaligned fields Ian Collins <ian-news@hotmail.com> - 2013-08-24 16:24 +1200
              Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-23 23:47 -0500
            Re: Struct with unaligned fields Keith Thompson <kst-u@mib.org> - 2013-08-24 01:16 -0700
              Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-24 09:45 -0500
                Re: Struct with unaligned fields Keith Thompson <kst-u@mib.org> - 2013-08-24 13:52 -0700
                Re: Struct with unaligned fields Tim Prince <tprince@computer.org> - 2013-08-24 17:07 -0400
                Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-24 16:35 -0500
                Re: Struct with unaligned fields Ian Collins <ian-news@hotmail.com> - 2013-08-25 09:51 +1200
                Re: Struct with unaligned fields Keith Thompson <kst-u@mib.org> - 2013-08-24 16:04 -0700
                Re: Struct with unaligned fields Les Cargill <lcargill99@comcast.com> - 2013-08-24 21:03 -0500
          Re: Struct with unaligned fields Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-24 08:22 -0400
  Re: Struct with unaligned fields Nobody <nobody@nowhere.com> - 2013-08-23 17:51 +0100
  Re: Struct with unaligned fields falk@rahul.net (Edward A. Falk) - 2013-08-24 01:43 +0000

csiph-web