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


Groups > comp.lang.c > #9821

Re: Structure with unsigned chars and internal alignment

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Structure with unsigned chars and internal alignment
Date 2011-09-01 08:34 -0700
Organization None to speak of
Message-ID <lnsjogs2cv.fsf@nuthaus.mib.org> (permalink)
References (1 earlier) <j3k3e6$a1h$1@dont-email.me> <j3kmj4$bt8$1@nnrp.ngi.it> <j3mn14$8at$1@dont-email.me> <j3n70j$qrf$1@nnrp.ngi.it> <j3njl3$lo5$1@dont-email.me>

Show all headers | View raw


"BartC" <bc@freeuk.com> writes:
> "pozz" <pozzugno@gmail.com> wrote in message
> news:j3n70j$qrf$1@nnrp.ngi.it...
>
>> Just to better explain the content of the file:
>>
>> - 2 bytes that code the size N of all the subsequent blocks
>> - N bytes for block 1
>> - N bytes for block 2
>> - ...up to the end of the file
>>
>> A single N-bytes block is composed by two field:
>> - 2 bytes for field1
>> - N-2 bytes for field2
>>
>> field1 and field2 are application data that aren't important now for our
>> discussion.
>
> So why is it necessary to read each block of N bytes in one go?
>
> Why not read 2 bytes into field1, then N-2 bytes into field2? Then problems
> of padding and alignment will disappear.

Those problems also disappear if you just read each block into an N-byte
array.

> (And field1 does sound very much sound like a 16-bit numeric value; if it 
> is, it might as well be declared as one, making it easier to work with.)

It's stored in big-endian format, so you can't just read it directly
into a 16-bit integer object (unless you use htons() and/or ntohs()).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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