Thread: Correct way to pass arguments to this function

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    Correct way to pass arguments to this function

    Hi,

    I have a standard Internet Checksum function as follows:

    Code:
    uint16_t checksum_f(uint16_t *addr, uint32_t length) {
    
    
    register long sum = 0;
    while (length > 1) {
    sum += * (unsigned short *) addr++;
    length -= 2;
    }
    if (length > 0)
    sum += * (unsigned char *) addr;
    while (sum >> 16)
    sum = (sum & 0xFFFF) + (sum >> 16);
    return ~sum
    }
    If I wanted to calculate a checksum for the following two things, how would I correctly pass in the parameters to the checksum function:

    Code:
    typedef struct DATE {
    
    
    uint16_t year; uint8_t month; uint8_t day;
    }
    Code:
    int main () {
    
    
    DATE tx *txPtr DATE *rxPtr uint8_t rx_buffer[100]; uint16_t checksum1, checksum2; txPtr = &tx; // this will get filled with date information at some point rxPtr = (DATE *)rx_buffer; // rx buffer contains a date // now assume there is data in each // what do you pass in to get the checksum of the data contained in the rx and tx structs? checksum1 = checksum_f(?, ?); checksum2 = checksum_f(?, ?);
    }
    Thanks!
    Last edited by eponymous; 03-18-2008 at 10:00 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    checksum_f(rxPtr , sizeof (DATE));

    I suppose
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Give this a try...
    Code:
    typedef struct {
    	uint16_t year;
    	uint16_t month;
    	uint16_t day;
    }DATE;
    Code:
    int main(void)
    {
    	DATE tx = {1976, 03, 27};
    	DATE *txPtr = {0};
    	DATE *rxPtr = {0};
    	uint16_t rx_buffer[100] = {1974, 02, 25};
    	uint16_t checksum1 = 0;
    	uint16_t checksum2 = 0;
    	uint16_t test[3] = {0};
    	txPtr = &tx; // this will get filled with date information at some point
    	rxPtr = (DATE *)rx_buffer; // rx buffer contains a date
    	memcpy(test, txPtr, sizeof DATE);
    	checksum1 = checksum_f(test, sizeof test);
    	memset(test, 0, sizeof test);
    	memcpy(test, rxPtr, sizeof DATE);
    	checksum2 = checksum_f(test, sizeof test);
    	return 0;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why do you need memcpy? Just a waste of CPU power moreover - if DATE has some padding - you are getting a buffer overrun
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by vart View Post
    why do you need memcpy? Just a waste of CPU power moreover - if DATE has some padding - you are getting a buffer overrun
    Because this will NOT work...
    checksum_f(rxPtr , sizeof (DATE));

    I suppose
    You're trying to use a DATE variable for the 1st parm of the function which requires an unsigned int pointer. Thus, I used memcpy to convert the DATE variable to a variable that is compatible with the function.
    Last edited by BobS0327; 03-19-2008 at 05:50 AM. Reason: clarification

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You're trying to use a DATE variable
    Not DATE but pointer to DATE - so simple casting will be enogh

    memcpy just overcomplicates things, and potentially (not very likely but still) could have some side effects due to padding
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by vart View Post
    Not DATE but pointer to DATE - so simple casting will be enogh

    memcpy just overcomplicates things, and potentially (not very likely but still) could have some side effects due to padding
    It really should be a unsigned int pointer not a pointer to DATE.

    This

    checksum_f(rxPtr , sizeof (DATE));
    should be this

    Code:
    checksum_f((uint16_t *) rxPtr , sizeof (DATE));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM