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


Groups > comp.lang.c > #30406

Re: Data alignment questin, structures

From mathog <mathog@caltech.edu>
Newsgroups comp.lang.c
Subject Re: Data alignment questin, structures
Date 2013-01-20 10:26 -0800
Organization A noiseless patient Spider
Message-ID <kdhct5$bmb$1@dont-email.me> (permalink)
References <kcs982$r3p$1@dont-email.me> <kcshq5$7e8$1@dont-email.me> <kdc426$f0i$1@dont-email.me> <kdftp6$i66$1@dont-email.me> <kdh9it$kjm$1@dont-email.me>

Show all headers | View raw


James Kuyper wrote:
> I've noticed that you routinely fail to provide citations for who wrote
> the things that you quoted.
Sorry. When the number of replies is more than 1 or 2 I can't ever 
remember who is >>>> and who is >>>>>> in any case.

That's rather rude to the persons who said
> it,, and also inconvenient for the rest of us trying to figure out who
> said what. It can also get you into legal trouble in some contexts,
> thought usenet posts are so easily forged that it would be hard to
> imagine that happening in this context.
>
> I deny permission to any and all authors to quote any text that I write
> without appropriate citation. Don't bother responding if you're not
> willing to provide it.
>
>> Taking into account the various answers in this thread to the preceding
>> question it would seem that there is still no safe (safe meaning here
>> "code that will work on any platform") method to use a C struct
>> to extract binary data directly from memory, since the compiler can
>> throw in padding and alignment requirements whenever it feels like it.
>
> Not quite - for instance, padding is not allowed at the beginning, and
> the alignment requirement for an aggregate object (a struct or an array)
> must be at least as strict as it is for any of the members of the
> aggregate. Still, the basic point remains - the standard does not
> provide enough guarantees to allow the use such a struct to read date
> directly from a file.
>
>> Even if the data is reduced to byte representation, something like this
>> (I know it isn't the same as Mystruct4 above):
>>
>>    typedef struct {
>>        uint8_t one[2];    /* actually uint16_t */
>>        uint8_t two[4];    /* actually uint32_t */
>>        int8_t  three[2];  /* etc. */
>>        int8_t four;
>>    } Mystruct4b;
>>
>> it seems not to be safe to pass a "Mystruct4b *" pointer to a function
>> which references this data at an arbitrary location in memory.  Instead
>> the only safe method is to pass "char *" pointers and take the data
>> apart with memcpy() at a very low level, moving it from memory to the
>> structure, or vice versa.
>
> Correct. Because of these things that are not guaranteed, C structs
> should be considered as suitable only for use for in-memory objects, not
> for reading or writing from files.
>
>> What an odd situation.  I would have thought with all of the data that
>> does show up in C programming as "a series of bytes in a buffer arranged
>> in some particular manner", that is, pretty much any data which is
>> passed from machine to machine, C would have developed a method for
>> simplifying this sort of code.  Perhaps something along these lines
>
> I agree that some such feature would be useful. But there are problems
> with the various possible alternatives.
>
>>    /* declare memory organization, no padding, no alignment requirement */
>>    typedef memstruct {
>>        uint16_t one;
>>        uint32_t two;
>>        int16_t  three;
>>        uint8_t  four;
>>    } Mystruct4c;
>>
>> Specifically so that one could pass a "Mystruct4c *" pointer to a
>> function, like so:
>>
>>      myfunction3a(Mystruct4c *ptr){
>>         ptr->two = 5;
>>         printf("value of three:%d\n",ptr->three);
>>      }
>
>
> The fundamental problem with such a feature is that &ptr->two is not
> necessarily correctly aligned for a uint32_t object.

Exactly.  memstruct != struct.  A memstruct is a memory structure 
designed for accessing nonaligned data, the field organization is 
verbatim and cannot be distorted by the compiler to add padding or 
require alignments.  In a memstruct "ptr->two" means some equivalent of:

    uint32_t tmp4;
    memcpy(&tmp4,ptr + offsetof(Mystruct4c,two),4);

especially including the cases where tmp4 is in a register, "memcpy" is 
some assembler operation(s) to copy from a random point in memory into 
that register, so that

   &(ptr->two)

would not be allowed.  Just like this:

   register uint32_t tmp4;
   printf("Pointer to:%p\n",&tmp4);

is not valid. Presumably casting a memstruct to a struct and vice versa 
would also be forbidden.  That was the point of:

 >  Mystruct4c  native;
 >  Mystruct4b *foreign
 >  /* field names/sizes must match for the following statement */
 >  alternate_representations {Mystruct4b, Mystruct4c}

which tells the compiler that it needs to know how to copy data in 
either direction between a specific memstruct and a specific struct. 
(And that would be illegal if the fields didn't match exactly.)

I am not worried about endian issues or other floating point 
representations.  Both of these can be dealt with easily enough once the 
data has been mapped from nonaligned to aligned data structures.  As for 
bytes that are not 8 bits, does any modern hardware exist where that is 
not true?  Must we keep the requirement to run on archaic hardware 
forever going forward with newer versions of the language?

Regards,

David Mathog

Back to comp.lang.c | Previous | Next — Previous in thread | Next 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