Re: Checksum in a struct

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

  1. Stefan Ram

    Stefan Ram Guest

    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
    #1
    1. Advertising

  2. Stefan Ram

    James Kuyper Guest

    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
    #2
    1. Advertising

  3. Stefan Ram

    James Kuyper Guest

    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
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sosman

    Re: Checksum in a struct

    Eric Sosman, Jul 11, 2012, in forum: C Programming
    Replies:
    12
    Views:
    636
    Tim Rentsch
    Jul 21, 2012
  2. Re: Checksum in a struct

    , Jul 11, 2012, in forum: C Programming
    Replies:
    0
    Views:
    334
  3. Re: Checksum in a struct

    , Jul 12, 2012, in forum: C Programming
    Replies:
    0
    Views:
    370
  4. Peter Nilsson

    Re: Checksum in a struct

    Peter Nilsson, Jul 13, 2012, in forum: C Programming
    Replies:
    0
    Views:
    316
    Peter Nilsson
    Jul 13, 2012
  5. Tim Rentsch

    Re: Checksum in a struct

    Tim Rentsch, Jul 21, 2012, in forum: C Programming
    Replies:
    0
    Views:
    327
    Tim Rentsch
    Jul 21, 2012
Loading...

Share This Page