This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Finding a complex structure size

I have tried variations using sizeof() with unreliable results. This method works, but is it bogus? Can it be improved? I am not worried about it being portable.

// -- Unit Variables --
struct {
  // Complex Structure
  // Lots of nested arrays,
  // integer values, etc.
} message;

char replyBuffer[20];	// Input Buffer


// Return size of message structure
unsigned int getMessageSize(void) {
   int i, *p1, *p2;

   p1 = (int *)&message;      // Create pointer to Message Struct
   p2 = (int *)&replyBuffer;  // Create pointer to replyBuffer
   i = p2-p1;		      // Calculate message structure size
   return(i);                 // Does this really work?
}

Parents
  • You are assuming that 'message' is always located before
    'replyBuffer', this is very dangerous. It will definitely
    not work if message and replyBuffer are located in
    different memory areas (near vs. far/huge etc.):

    typedef struct message  {
      // Complex Structure
      // Lots of nested arrays,
      // integer values, etc.
    } MESSAGE;
    
    MESSAGE          msg;
    char     replyBuffer[20];	// Input Buffer
    
    // Return size of message structure
    unsigned int getMessageSize(void) {
      return (sizeof (msg));
    }
    

    If sizeof does not give the expected results, then this needs to be addressed. The message structure probably has alignment constraints, that is gaps between members. Those gaps are required to ensure that int/uint/long etc. members are properly aligned to 16-bit boundaries. You may also use 'pragma pack()' to avoid alignment at the expense of an increase in code size:

    #pragma pack(1) // or: bytealign
    typedef struct message  {
      // Complex Structure
      // Lots of nested arrays,
      // integer values, etc.
    } MESSAGE;
    #pragma pack()  // restore default packing (pack(2))
    

Reply
  • You are assuming that 'message' is always located before
    'replyBuffer', this is very dangerous. It will definitely
    not work if message and replyBuffer are located in
    different memory areas (near vs. far/huge etc.):

    typedef struct message  {
      // Complex Structure
      // Lots of nested arrays,
      // integer values, etc.
    } MESSAGE;
    
    MESSAGE          msg;
    char     replyBuffer[20];	// Input Buffer
    
    // Return size of message structure
    unsigned int getMessageSize(void) {
      return (sizeof (msg));
    }
    

    If sizeof does not give the expected results, then this needs to be addressed. The message structure probably has alignment constraints, that is gaps between members. Those gaps are required to ensure that int/uint/long etc. members are properly aligned to 16-bit boundaries. You may also use 'pragma pack()' to avoid alignment at the expense of an increase in code size:

    #pragma pack(1) // or: bytealign
    typedef struct message  {
      // Complex Structure
      // Lots of nested arrays,
      // integer values, etc.
    } MESSAGE;
    #pragma pack()  // restore default packing (pack(2))
    

Children
No data