Re: Checksum in a struct

Discussion in 'C Programming' started by fmassei@gmail.com, Jul 11, 2012.

  1. Guest

    On Wednesday, July 11, 2012 4:56:53 PM UTC+2, pozz wrote:
    > I have a function that computes a 16-bit checksum (following whatever
    > algorithm) of a memory space:
    >
    > unsigned int checksum(const void *buffer, size_t size);
    >
    > I want to embed this checksum in a struct:
    >
    > struct PStruct {
    > int x;
    > unsigned int y;
    > char z[13];
    > ...
    > unsigned int checksum;
    > };
    >
    > How to use the checksum() function above? I propose:
    >
    > struct PStruct ps;
    > ...
    > ps.checksum = checksum(&ps, offsetof(struct PStruct, checksum));
    >
    > Is there a better mechanism?


    It's ok, as long as you check the checksum with the same program on the same machine: should not that be the case, I suggest you to write a specific (maybe endian-independent?) CRC for that struct: you want to skip the the paddings and be indipendent from the specific encoding of ints/doubles).
    Ciao!
    , Jul 11, 2012
    #1
    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:
    630
    Tim Rentsch
    Jul 21, 2012
  2. Stefan Ram

    Re: Checksum in a struct

    Stefan Ram, Jul 11, 2012, in forum: C Programming
    Replies:
    2
    Views:
    361
    James Kuyper
    Jul 25, 2012
  3. Re: Checksum in a struct

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

    Re: Checksum in a struct

    Peter Nilsson, Jul 13, 2012, in forum: C Programming
    Replies:
    0
    Views:
    312
    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:
    325
    Tim Rentsch
    Jul 21, 2012
Loading...

Share This Page