determining the size of a structure

Discussion in 'C++' started by Luca, Aug 1, 2003.

  1. Luca

    Luca Guest

    I have the following problem:

    I'm developing a system where there are some processes that
    communicate each other via message queues; the message one process can
    send to another process is as follows:
    ******************************************
    struct ST_MSG {
    int iType;
    char aData[MAX_DATA_PART_LENGTH];
    }
    ******************************************
    the message is composed of two parts: iType tells about the kind of
    message and the aData buffer contains the informative part.
    depending on the value assumed by iType, the aData buffer has to be
    cast explicitely to the proper structure.

    the problem I have is that of determining the value of
    MAX_DATA_PART_LENGTH

    I have tried using the #define macro of the preprocessor but, due to
    the high number of structures, the compiler crashes.

    I wouldn't like to use an off-line solution such as building a
    separate project whose only job is that of calculating the
    MAX_DATA_PART_LENGTH number.

    I would like to solve the problem during the compilation of my prject

    do you have any idea?

    Thank you

    Luca
     
    Luca, Aug 1, 2003
    #1
    1. Advertising

  2. Luca

    ES Kim Guest

    "Luca" <> wrote in message
    news:...
    > I have the following problem:
    >
    > I'm developing a system where there are some processes that
    > communicate each other via message queues; the message one process can
    > send to another process is as follows:
    > ******************************************
    > struct ST_MSG {
    > int iType;
    > char aData[MAX_DATA_PART_LENGTH];
    > }
    > ******************************************
    > the message is composed of two parts: iType tells about the kind of
    > message and the aData buffer contains the informative part.
    > depending on the value assumed by iType, the aData buffer has to be
    > cast explicitely to the proper structure.
    >
    > the problem I have is that of determining the value of
    > MAX_DATA_PART_LENGTH
    >
    > I have tried using the #define macro of the preprocessor but, due to
    > the high number of structures, the compiler crashes.
    >
    > I wouldn't like to use an off-line solution such as building a
    > separate project whose only job is that of calculating the
    > MAX_DATA_PART_LENGTH number.
    >
    > I would like to solve the problem during the compilation of my prject
    >
    > do you have any idea?
    >
    > Thank you
    >
    > Luca


    You mean MAX_DATA_PART_LENGTH is not constant,
    but must be calculated before a struct variable is created, right?
    Then using a string can be a solution.

    struct ST_MSG {
    int iType;
    string aData;
    ST_MSG(int max) : iType(0), aData(string(max, 0)) { }
    };

    To create an object

    int max = /* calculate max somehow */
    ST_MSG s(max);

    You can access each chars in aData as if it's an array, such as s.aData[10]
    --
    ES Kim
     
    ES Kim, Aug 1, 2003
    #2
    1. Advertising

  3. Luca wrote:

    > I have the following problem:
    >
    > I'm developing a system where there are some processes that
    > communicate each other via message queues; the message one process can
    > send to another process is as follows:
    > ******************************************
    > struct ST_MSG {
    > int iType;
    > char aData[MAX_DATA_PART_LENGTH];
    > }
    > ******************************************
    > the message is composed of two parts: iType tells about the kind of
    > message and the aData buffer contains the informative part.
    > depending on the value assumed by iType, the aData buffer has to be
    > cast explicitely to the proper structure.


    I hope you don't actually intend to cast it. That is unlikely to result
    in correct, portable, well-defined code.

    >
    > the problem I have is that of determining the value of
    > MAX_DATA_PART_LENGTH
    >
    > I have tried using the #define macro of the preprocessor but, due to
    > the high number of structures, the compiler crashes.


    Sounds like you need a better compiler. Anyway, you should use const
    rather than #define for creating constants.

    Your actual question doesn't make much sense. You can't create or use
    instances of your structure without knowing the size. In the example
    above, the size is MAX_DATA_PART_LENGTH. That should be sufficient for
    determining the size. You could also use sizeof(msg.aData) where msg is
    an instance of ST_MSG.

    >
    > I wouldn't like to use an off-line solution such as building a
    > separate project whose only job is that of calculating the
    > MAX_DATA_PART_LENGTH number.
    >
    > I would like to solve the problem during the compilation of my prject


    Maybe I misunderstood the question at first. If you are asking what
    value to give MAX_DATA_PART_LENGTH, then I think it depends heavily on
    what you are doing with it. If you have structs representing each of the
    possible message types, you could create a union of all of them:

    union SizeUnion
    {
    StringMessage s;
    LongMessage l;
    //...
    };

    Then the maximum size required to store any of those structures is
    sizeof(SizeUnion).

    However, this is not necessarily a good solution. It relies on the
    assumption that messages are no larger than the structures that are used
    to represent them. This may be the case if you simply memcpy the data
    from a struct into a char array and pass that along, but this is not a
    very good solution to the problem - it is inflexible, confining,
    non-portable (in the sense that structs may not be the same across
    platforms or compilers), lends itself to errors (such as alignment
    problems), difficult to extend, and sloppy.

    The "correct" way to handle something like this is to define a protocol
    for serializing your message types. That protocol would include details
    about the sizes of the messages. This doesn't allow you to determine the
    size at compile time, since the size is determined by the protocol. But
    implemented correctly it is much easier to deal with, much more
    flexible, and should give nice, clean results.

    -Kevin
     
    Kevin Goodsell, Aug 1, 2003
    #3
    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. Gunnar Hjalmarsson

    determining file size

    Gunnar Hjalmarsson, Aug 13, 2004, in forum: Perl
    Replies:
    2
    Views:
    449
    J├╝rgen Exner
    Aug 13, 2004
  2. SamIAm
    Replies:
    0
    Views:
    418
    SamIAm
    Feb 9, 2004
  3. Kislay

    Size of a structure : Structure Padding

    Kislay, Oct 1, 2007, in forum: C Programming
    Replies:
    15
    Views:
    987
    clinuxpro
    Jul 13, 2011
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,736
    Michael Jung
    May 25, 2008
  5. Jeremy

    Determining document structure

    Jeremy, Jan 29, 2007, in forum: Javascript
    Replies:
    5
    Views:
    84
    Jeremy
    Jan 31, 2007
Loading...

Share This Page