# Re: Checksum in a struct

Discussion in 'C Programming' started by Stefan Ram, Jul 11, 2012.

1. ### Stefan RamGuest

pozz <>writes:
>struct PStruct {
>int x;
>unsigned int y;
>char z[13];
>...
>unsigned int checksum;
>};
>ps.checksum = checksum(&ps, offsetof(struct PStruct, checksum));

struct a
{ int x;
unsigned int y;
char z[12]; };

struct p
{ struct a a;
unsigned int checksum; };

....

p.checksum = checksum( &a, sizeof( a ));

or

struct p
{ unsigned int checksum;
struct a a; }

checksum( p, sizeof( a ));

Stefan Ram, Jul 11, 2012

2. ### James KuyperGuest

On 07/12/2012 10:42 AM, pozz wrote:
> Il 11/07/2012 18:32, Stefan Ram ha scritto:
>> pozz <>writes:
>>> struct PStruct {
>>> int x;
>>> unsigned int y;
>>> char z[13];
>>> ...
>>> unsigned int checksum;
>>> };
>>> ps.checksum = checksum(&ps, offsetof(struct PStruct, checksum));

>>
>> struct a
>> { int x;
>> unsigned int y;
>> char z[12]; };
>>
>> struct p
>> { struct a a;
>> unsigned int checksum; };
>>
>> ...
>>
>> p.checksum = checksum( &a, sizeof( a ));
>>
>> or
>>
>> struct p
>> { unsigned int checksum;
>> struct a a; }
>>
>> checksum( p, sizeof( a ));
>>

>
> What is the advantage of your method?

The sizeof expression is simpler than the corresponding offsetof()
macro. It can be simplified even further: the parentheses around the 'a'
are unnecessary.
The corresponding cost is that the definition and use of struct a and
struct p are, together, more complicated than your definition of struct
PStruct.

James Kuyper, Jul 12, 2012

3. ### James KuyperGuest

On 07/15/2012 03:58 AM, pozz wrote:
> Il 12/07/2012 17:07, James Kuyper ha scritto:
>> On 07/12/2012 10:42 AM, pozz wrote:
>>> Il 11/07/2012 18:32, Stefan Ram ha scritto:
>>>> pozz <>writes:
>>>>> struct PStruct {
>>>>> int x;
>>>>> unsigned int y;
>>>>> char z[13];
>>>>> ...
>>>>> unsigned int checksum;
>>>>> };
>>>>> ps.checksum = checksum(&ps, offsetof(struct PStruct, checksum));
>>>>
>>>> struct a
>>>> { int x;
>>>> unsigned int y;
>>>> char z[12]; };
>>>>
>>>> struct p
>>>> { struct a a;
>>>> unsigned int checksum; };
>>>>
>>>> ...
>>>>
>>>> p.checksum = checksum( &a, sizeof( a ));
>>>>
>>>> or
>>>>
>>>> struct p
>>>> { unsigned int checksum;
>>>> struct a a; }
>>>>
>>>> checksum( p, sizeof( a ));
>>>>
>>>
>>> What is the advantage of your method?

>>
>> The sizeof expression is simpler than the corresponding offsetof()
>> macro.

>
> Anyway I think this is a compilation-time complexity. ...

What I was concerned with was composition-time complexity: the time it
takes the author to write the expression in the source code. It's also
maintenance-time complexity: the time it takes the reader to read and
understand the code.

> ... Both
> offsetof() and sizeof() should be calculated at compilation time... is
> it correct?

Basically. They aren't required to be calculated at compile time; the C
standard doesn't make such distinctions. However, they can both be
computed at compile time, and it's a reasonable thing to expect of an
implementation.
--
James Kuyper

James Kuyper, Jul 25, 2012