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
  • Sizeof() is a compile-time constant. If sizeof() has a bug, then the compiler doesn't know big your stuctures are, which means that it's highly likely that the linker won't allocate space for them properly. In short, your program most likely wouldn't work correctly.

    Why does

    unsigned int getMessageSize ()
        {
        return sizeof(message);
        }
    

    not work? Where does it differ from the result you posted? I prefer to typedef all structures rather than have anonymous ones running around, but that shouldn't affect your problem. If you stick with your original method, you might at least declare a larger structure to avoid problems with order of variables:

    typedef struct
        {
        // complicated declarations
        } Message;
    
    struct
        {
        Message m;
        U8      justBeyondM;
        } SizeofMessage;
    

    And then do your math. The offsetof() macro will come in handy; no actual variables need be allocated. It should be in the standard library. If not,
    &(((SizeofMessage*)0)->justBeyondM)
    

Reply
  • Sizeof() is a compile-time constant. If sizeof() has a bug, then the compiler doesn't know big your stuctures are, which means that it's highly likely that the linker won't allocate space for them properly. In short, your program most likely wouldn't work correctly.

    Why does

    unsigned int getMessageSize ()
        {
        return sizeof(message);
        }
    

    not work? Where does it differ from the result you posted? I prefer to typedef all structures rather than have anonymous ones running around, but that shouldn't affect your problem. If you stick with your original method, you might at least declare a larger structure to avoid problems with order of variables:

    typedef struct
        {
        // complicated declarations
        } Message;
    
    struct
        {
        Message m;
        U8      justBeyondM;
        } SizeofMessage;
    

    And then do your math. The offsetof() macro will come in handy; no actual variables need be allocated. It should be in the standard library. If not,
    &(((SizeofMessage*)0)->justBeyondM)
    

Children