Thread: Communication protocol implementation : Array vs Structure

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Communication protocol implementation : Array vs Structure

    Please advise which coding style approach is better: Variant A or Variant B?
    Major question is whether to use arrays or structure in example function below.
    Thank you for advice.

    Common definitions:

    Code:
    typedef struct MsgHeader
    {
        int MagicNumber;
        int OpCode;
        int DataSize;
        int Checksum;
        int Err;
    }
    MsgHeader;
    
    typedef struct
    {
        MsgHeader hdr;
        int data[10];
    }
    CommBlock;
                
    typedef struct
    {
        int nMinorVersion ;
        int nMajorVersion ;
        int nBuildNumber ;
    }
    CmdGetVersion;
    Variant A:
    Code:
    int GetVersion(CommBlock* pCommand)
    {
        int enErr = eVMD_NOERROR ;
        
        // Check op code
        if(pCommand->hdr.OpCode != eVMD_GET_VERSION )
        {
            enErr = eVMD_BAD_OPCODE ;
        }
    
        pCommand->hdr.DataSize = 3 ;
                    
        pCommand->data[VMD_CMD_GET_VERSION_0] = VMD_MINOR_VERSION ; // Minor VMD software version
        pCommand->data[VMD_CMD_GET_VERSION_1] = VMD_MAJOR_VERSION ; // Major VMD software version
        pCommand->data[VMD_CMD_GET_VERSION_2] = VMD_BUILD_NUMBER ;  // VMD build number   
        
        return enErr ;
    }
    Variant B:
    Code:
    int GetVersion(CommBlock* pCommand )
    {
        int enErr = eVMD_NOERROR ;
        CmdGetVersion objReply ;
       
        memset(&objReply, 0, sizeof(CmdGetVersion ) ;
        
        // Check op code
        if(pCommand->hdr.OpCode != eVMD_GET_VERSION )
        {
            enErr = eVMD_BAD_OPCODE ;
        }
    
        pCommand->hdr.DataSize = 3 ;
                    
        objReply.nMinorVersion = VMD_MINOR_VERSION ; // Minor VMD software version
        objReply.nMajorVersion = VMD_MAJOR_VERSION ; // Major VMD software version
        objReply.nBuildNumber = VMD_BUILD_NUMBER ;  // VMD build number   
        
        memcpy(pCommand->data, &objReply, sizeof(CmdGetVersion ) ;
    
        return enErr ;
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Assuming they behave the same (it looks like they do) it doesn't matter: Variant A doesn't copy data from a temporary area to the final destination -- but the compiler will optimize it out of Variant B anyway.

    If you must choose one for performance reasons: measure!
    If you must choose one for resource usage reasons: measure!
    If you must choose one for electricity usage reasons: measure!

    Style wise: I prefer Variant A, but use whichever you feel more confortable with.
    Last edited by qny; 09-11-2012 at 08:22 AM. Reason: missed the style point

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Data structure alignment - Wikipedia, the free encyclopedia
    Endianness - Wikipedia, the free encyclopedia
    If you're trying to communicate this information over some external interface (be it a file, network, or shared memory), then these issues will bite you at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if you use a struct you have to be sure the layout of the struct is consistent across platforms. When you declare a struct, the compiler implementation can decide to pad the struct for alignment purposes depending on the data types in the struct. so the layout of the variables might not be contiguous. if the other side of your protocol is on a different machine with a different compiler it might lay out the structure differently. you could use a 'pack' directive to force out the padding but even that is implementation dependent. In this case since the struct contains all ints, it probably would be ok, but that is just an assumption. So for actually loading data into a packet, using an array is more reliable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-08-2011, 08:31 AM
  2. Webdav Protocol implementation
    By ujwala in forum C Programming
    Replies: 1
    Last Post: 03-20-2010, 06:21 AM
  3. qmail+fetchmail array communication
    By CornedBee in forum Tech Board
    Replies: 5
    Last Post: 01-04-2005, 05:04 AM
  4. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  5. hash table data structure implementation
    By sanju in forum C Programming
    Replies: 1
    Last Post: 09-27-2002, 05:06 AM