Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Structure with unsigned chars and internal alignment |
| Date | 2011-08-31 21:26 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <j3mn14$8at$1@dont-email.me> (permalink) |
| References | <j3jmrn$cae$1@nnrp.ngi.it> <j3k3e6$a1h$1@dont-email.me> <j3kmj4$bt8$1@nnrp.ngi.it> |
On 8/31/2011 3:07 AM, pozz wrote:
> Il 31/08/2011 03:39, Eric Sosman ha scritto:
>>> In this case, in my application I'd like to read the size of myStruct
>>> (between 3 and 32) from a file. field1 will be always 2-bytes long,
>>> field2 will be the size of myStruct minus 2 bytes of field1 (in the case
>>> no padding is present between field1 and field2).
>>
>> You probably do *not* want to read "the size of myStruct" from
>> the file; you want to read "the size of some blob of data." The two
>> environments (on-disk form and in-memory form) are not necessarily
>> identical, even if they're strongly related by intention.
>
> This is my case.
>
>
>> I said "probably," because perhaps your file actually does hold
>> "the size of myStruct." This could be the case if an actual `struct
>> myStruct' was written to the file originally, complete with whatever
>> padding it might have included. If you never, never need to move the
>> data to another system (not even for post-mortem analysis), you can
>> probably get away with this.
>
> I understand your point and I expaling what I'm trying to do.
I'm not so sure you understand my points. For myself, I'm
*sure* I don't understand "expaling."
> I have to read a file, created by another application on another
> platform, that is composed by blocks of data (what you named "blob of
> data"). The size of these blocks (between 3 and 32) is written in the
> same file at the beginning.
> A single block is composed by 2 bytes and (block_size - 2) bytes.
You haven't said so, but I guess that the initial two bytes
somehow encode `block_size'. Whether the remaining bytes are all
"payload" or may themselves include padding may be known to you, but
remains a mystery to the rest of us.
> Because I don't know the size of blocks I'll read and I can't malloc the
> right size at run-time, I was trying to define the maximum size of
> block, splitting it in the two fields:
Again the hangup over the absence of malloc(). If this has
anything at all to do with the problem, it has to do with some aspect
of the problem that you have not yet revealed. Based on what you've
said and shown, the existence or non-existence of malloc() has zilch
to do with the matter.
> struct myStruct {
> unsigned char field1[2];
> unsigned char field2[30];
> };
... but here comes the "splitting it in the two fields" part,
which you're going about (as several people have told you) in an
unreliable way.
> I thought I could have read the block and copy it directly to myStruct.
> Anyway if padding could be present in myStruct, I can't use this approach.
>
> Maybe the best approach is:
>
> #define FIELD1_OFFSET 0
> #define FIELD1_SIZE 2
> #define FIELD2_OFFSET FIELD1_SIZE
> #define BLOCK_MAXSIZE 32
> void read_block(struct myStruct *s, size_t block_size) {
> unsigned char block[BLOCK_MAXSIZE];
> <read BLOCK_MAXSIZE bytes and copy it in block array>
> memcpy(s->field1, &block[FIELD1_OFFSET], FIELD1_SIZE);
> memcpy(s->field2, &block[FIELD2_OFFSET], block_size - FIELD1_SIZE);
> }
>
> Here block_size is passed as an argument, because I don't know it in
> advance.
That's odd. Where do you learn `block_size', *before* reading
the first two bytes of your blob? And if `block_size' turns out to
be less than thirty-two, how does your "read BLOCK_MAXSIZE bytes"
avoid running off the end and into whatever follows the blob?
Observation: It is premature to seek the "best" way to do
something when you have not yet come up with "any" way. That's
premature optimization personified.
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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