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


Groups > comp.lang.c > #30084

Re: Data alignment questin, structures

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: Data alignment questin, structures
Date 2013-01-13 17:16 -0500
Organization A noiseless patient Spider
Message-ID <kcvboc$onv$1@dont-email.me> (permalink)
References <kcs982$r3p$1@dont-email.me> <kcsp35$2tq$1@dont-email.me> <50F2FA23.1060004@caltech.edu>

Show all headers | View raw


On 1/13/2013 13:17, mathog wrote:
> Shao Miller wrote:
>
>> If you have implementation-specific knowledge of what will happen, then
>> I'd suggest that you do whatever you want to accomplish your goal.
>
> That's the point - I want this code to work wherever, without having
> access to all possible platforms to test it first.  Implicitly
> implementation specific is exactly what I am trying to avoid.
>
>>
>> If you actually care about portability (you seem to), I'd suggest
>> formally serializing and deserializing data to and from the file.
>
> Yeah, I was afraid that was going to be the only standard compliant way
> of doing this.  The key thing that I have learned is that passing a
> pointer to an unaligned structure isn't portable (6.3.2.3p7 cited by
> Eric Sosnan

(Sosman)

> earlier in this thread).  There are some instances of that
> in the code that swaps bytes (big endian to little endian), so those
> routines will need to have their interfaces adjusted.  They are
> otherwise "serial".  Better to do this now, before the code is released,
> than later.

Based on what you've typed, I am guessing that the file format is not 
intended to be portable across different platforms.  Is that right? 
That is, copying a saved file that was made on an x86 isn't expected to 
load properly in the same program on PowerPC?  I ask because the padding 
within structures might also be a concern, for you.

By formally serializing/deserializing, you can have a portable file format.

> It also seems unavoidable that explicit recordtype_get()
> functions will be needed, with the data passed by void * or char *.
>
>> You might be interested in modern C, which is C11.  It includes the
>> '_Alignof' and '_Alignas' keywords, which allow you to consider and
>> choose alignments, respectively.
>
> This doesn't help with the struct offset problem, since we do not know
> the offset ahead of time.  In a particular file the struct could be
> anywhere in the buffer, at 100,102,..,500,502,etc.  Or am I
> misunderstanding the purpose of the _Alignof and _Alignas?

Ah, no.  If the structure can appear at any offset in the buffer, then 
these don't help you as much, unless the offset happens to be a multiple 
of the alignment requirement.  But as mentioned, you can 'memcpy' from 
the offset into an aligned location, which you might be interested in 
doing anyway, if a structure can be truncated by being near the end of 
your buffer.

At least you can automate some of the serializing/deserializing...  You 
can have:

   struct s_foo {
       double d;
       int i;
       char c;
     };

   size_t foo_offsets[] = {
       offsetof(struct s_foo, d),
       offsetof(struct s_foo, i),
       offsetof(struct s_foo, c),
     };

   f_serialize * foo_serials[] = {
       serialize_double,
       serialize_int,
       serialize_char,
     };

where 'f_serialize' is a function typedef for a serialization function. 
  Then you can iterate through these arrays with a loop in a generic 
"structure serialization" function, instead of having a monolithic:

   void serialize_foo(struct s_foo * foo) {
       serialize_double(&foo->d);
       serialize_int(&foo->i);
       serialize_char(&foo->c);
     }

for each of your different structure types.

Or, here is one library you might be interested in:

   http://www.leonerd.org.uk/code/libpack/intro.html

-- 
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily,"  -- Richard Harter

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


Thread

Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-12 10:15 -0800
  Re: Data alignment questin, structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-12 18:54 +0000
    Re: Data alignment questin, structures Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-01-15 20:14 +0000
      Re: Data alignment questin, structures Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-15 16:29 -0500
  Re: Data alignment questin, structures Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-12 15:41 -0500
    Re: Data alignment questin, structures mathog <dmathog@gmail.com> - 2013-01-18 10:24 -0800
      Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-18 14:05 -0500
      Re: Data alignment questin, structures Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-18 14:46 -0500
      Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-18 21:11 +0000
      Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-18 17:14 -0500
        Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-18 23:09 +0000
      Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-18 16:23 -0800
      Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-19 21:02 -0800
        Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-20 01:25 -0500
        Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-20 07:53 +0000
        Re: Data alignment questin, structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-20 14:02 +0000
        Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-20 07:35 -0800
          Re: Data alignment questin, structures 88888 Dihedral <dihedral88888@googlemail.com> - 2013-01-20 11:49 -0800
        Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-20 12:30 -0500
          Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-20 10:26 -0800
            Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-20 15:11 -0500
            Re: Data alignment questin, structures "BartC" <bc@freeuk.com> - 2013-01-20 20:34 +0000
            Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-20 15:53 -0500
              Re: Data alignment questin, structures 88888 Dihedral <dihedral88888@googlemail.com> - 2013-01-20 13:16 -0800
              Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-20 20:27 -0800
                Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-21 00:37 -0500
                Re: Data alignment questin, structures Dr Nick <nospam-4@temporary-address.org.uk> - 2013-01-21 10:07 +0000
                Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-21 10:21 -0800
                Re: Data alignment questin, structures Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-21 10:34 -0800
                Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-21 13:07 -0800
                Re: Data alignment questin, structures "BartC" <bc@freeuk.com> - 2013-01-21 22:24 +0000
                Re: Data alignment questin, structures mathog <dmathog@gmail.com> - 2013-01-22 10:18 -0800
                Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-22 20:57 +0000
                Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-22 14:51 -0800
                Re: Data alignment questin, structures Ian Collins <ian-news@hotmail.com> - 2013-01-22 11:12 +1300
                Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-21 20:53 +0000
                Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-20 22:06 -0800
                Re: Data alignment questin, structures Philip Lantz <prl@canterey.us> - 2013-01-22 00:27 -0800
                Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-22 06:44 -0500
                Re: Data alignment questin, structures Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> - 2013-01-21 10:19 +0000
            Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-20 22:19 +0000
          Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-20 22:14 +0000
          Re: Data alignment questin, structures "BartC" <bc@freeuk.com> - 2013-01-20 22:48 +0000
            Re: Data alignment questin, structures Les Cargill <lcargill99@comcast.com> - 2013-01-20 18:11 -0600
            Re: Data alignment questin, structures Ian Collins <ian-news@hotmail.com> - 2013-01-21 14:07 +1300
  Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-12 17:45 -0500
    Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-13 10:17 -0800
      Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-13 17:16 -0500

csiph-web