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


Groups > comp.lang.c > #35671

Re: Struct with unaligned fields

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Struct with unaligned fields
Date 2013-08-24 11:47 +1200
Message-ID <b7qakkFeg9dU3@mid.individual.net> (permalink)
References <kv5jvm$7mk$1@dont-email.me> <kv5vgt$cj2$1@dont-email.me> <kv7j0j$a96$1@dont-email.me> <kv7lcm$ll4$1@dont-email.me> <kv8clj$mn5$1@dont-email.me>

Show all headers | View raw


James Harris wrote:
> "Eric Sosman" <esosman@comcast-dot-net.invalid> wrote in message
> news:kv7lcm$ll4$1@dont-email.me...
>> On 8/23/2013 8:03 AM, James Harris wrote:
>
> ....
>
>>> 1. Once read from a disk into memory such a structure can be read and
>>> written many times between CPU cache and CPU before being flushed back to
>>> disk or dropped.
>>
>>      How many times is "many times?"  If you can count as high as
>> "many" with an unsigned short, don't worry.
>>
>>      Also, if you don't need the field-at-a-time approach you use
>> one count them one conversion on input, and one count them one on
>> output (but only if you write).  The time spent on one count them
>> one decode and one count them one encode is zero count it zero,
>> for all practical purposes.
>
>  From this and other comments you have made I think you may be thinking of
> these awkward fields as being external to the running system - i.e. read it
> once and then forget about the original. That is true of some. But others
> may form elements of the running system. At least if there's a chance of a
> user program accessing any one of these fields we've been talking about I
> cannot just maintain my own copy until the whole thing is written back to
> disk. If I did, user programs would see inconsistent values.

These lower level, inherently non-portable details should be abstracted 
form user-land code. They would normally be locked away in the bowels of 
the filesystem layer and inaccessible to the user.  Otherwise the driver 
code can't make useful optimisations based on exclusive access to the 
raw data.  If you are not writing a filesystem or driver, why are you 
fiddling with this data?

> I suppose I could maintain a separate copy and try to come up with some way
> to detect whether something else is accessing the original. If it is then
> switch into a different mode where I maintain both at the same time. But
> that's much too complicated. It seems far simpler to maintain the original
> structure all the time.

See above.

> In fact I might be able to work with copies in some cases and I should
> consider doing so. However, I'd rather start from knowing I have a good way
> to deal with the most akward cases. Once those problems have been solved the
> rest gets easier. If I were to make assumptions at an early stage that I
> could align all fields it would then be harder later to work with those that
> could not be copied and aligned.

As the other posters have said, you can either decode the blob into a 
struct in in pass, or provide access functions for the individual 
fields.  Even if the data were aligned, you would still have the same 
issues regarding exclusive or non-exclusive access to the data.  So the 
bigger picture problem isn't much different.

It'll probably go down like a lead balloon here, but this is one case 
where C++ will give you a much more elegant solution!  You could 
abstract all of the messy details behind what appears (to client code) 
to be a conventional struct.

-- 
Ian Collins

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


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 Stephen Sprunk <stephen@sprunk.org> - 2013-08-29 14:30 -0500
                Re: Struct with unaligned fields Robert Wessel <robertwessel2@yahoo.com> - 2013-08-29 16:41 -0500
                Re: Struct with unaligned fields Stephen Sprunk <stephen@sprunk.org> - 2013-09-01 23:13 -0500
                Re: Struct with unaligned fields Richard Damon <Richard@Damon-Family.org> - 2013-09-02 09:48 -0400
                Re: Struct with unaligned fields David Thompson <dave.thompson2@verizon.net> - 2013-09-04 00:01 -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