Question about variable length structure

Discussion in 'C Programming' started by Wen, Mar 28, 2011.

  1. Wen

    Wen Guest

    Hi all,

    I have a structure with variable length information. To properly
    define it, I use the following data structure:

    typedef struct
    {
    HeaderInfo* headerInfo;
    ContentInfo* contentInfo;
    } PacketInfo;

    typedef struct
    {
    UInt8 numNeighbors;
    NeighborInfo* neighborInfo;
    } HeaderInfo;

    typedef struct
    {
    UInt8 ip;
    UInt8 port;
    } NeighborInfo;

    typedef struct
    {
    Char* content;
    } ContentInfo;

    Basically the situation is: both header and content are variable
    length, which can only be determined during runtime.
    Following the definition of those structures (assuming those are
    properly defined), how can I allocate memory to define an instance of
    Packet? And how do I free the memory? Thanks a lot in advance!
    Wen, Mar 28, 2011
    #1
    1. Advertising

  2. Wen

    Tom St Denis Guest

    On Mar 28, 6:00 am, Wen <> wrote:
    > Hi all,
    >
    > I have a structure with variable length information. To properly
    > define it, I use the following data structure:
    >
    > typedef struct
    > {
    >   HeaderInfo* headerInfo;
    >   ContentInfo* contentInfo;
    >
    > } PacketInfo;
    >
    > typedef struct
    > {
    >   UInt8 numNeighbors;
    >   NeighborInfo* neighborInfo;
    >
    > } HeaderInfo;
    >
    > typedef struct
    > {
    >   UInt8 ip;
    >   UInt8 port;
    >
    > } NeighborInfo;
    >
    > typedef struct
    > {
    >   Char* content;
    >
    > } ContentInfo;
    >
    > Basically the situation is: both header and content are variable
    > length, which can only be determined during runtime.
    > Following the definition of those structures (assuming those are
    > properly defined), how can I allocate memory to define an instance of
    > Packet? And how do I free the memory? Thanks a lot in advance!


    You will want to create a "AllocatePacket()" function where you pass
    it the number of neighbours and the content size.

    If you don't know these in advance then just pick reasonable limits
    and do bounds checks as you start filling it out.

    I wouldn't try to just do void *p = malloc(sizeof PacketInfo) ...

    Tom
    Tom St Denis, Mar 28, 2011
    #2
    1. Advertising

  3. Wen <> writes:

    > I have a structure with variable length information. To properly
    > define it, I use the following data structure:


    I don't think I can answer your question directly because I suspect
    you've asked the wrong one but I can offer a few thoughts:

    > typedef struct
    > {
    > HeaderInfo* headerInfo;
    > ContentInfo* contentInfo;
    > } PacketInfo;


    Do you know why you (or whoever) has decided these should be pointers?
    If you can't give a good reason why, you might consider changing that
    decision. Roughly speaking, every * in a data structure adds complexity
    to the code, but it can bring crucial benefits.

    > typedef struct
    > {
    > UInt8 numNeighbors;
    > NeighborInfo* neighborInfo;
    > } HeaderInfo;


    You might consider using a flexible array member here. That, again,
    will reduce the complexity of the allocation and de-allocation but it
    might not be suitable. There's too little information to tell.

    > typedef struct
    > {
    > UInt8 ip;
    > UInt8 port;
    > } NeighborInfo;


    I'm guessing that either the types or the members are badly named.

    > typedef struct
    > {
    > Char* content;
    > } ContentInfo;


    What does this do? What type is Char? Is it a typo for char? If so,
    that raises even more questions.

    > Basically the situation is: both header and content are variable
    > length, which can only be determined during runtime.
    > Following the definition of those structures (assuming those are
    > properly defined), how can I allocate memory to define an instance of
    > Packet? And how do I free the memory? Thanks a lot in advance!


    There is no object type called Packet so the question is not answerable
    as you state it. Did you mean PacketInfo?

    --
    Ben.
    Ben Bacarisse, Mar 28, 2011
    #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. Mitchua
    Replies:
    5
    Views:
    2,715
    Eric J. Roode
    Jul 17, 2003
  2. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,937
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  3. Sam
    Replies:
    3
    Views:
    14,081
    Karl Seguin
    Feb 17, 2005
  4. Andrew Smallshaw

    Variable structure, constant length allocations

    Andrew Smallshaw, Nov 10, 2008, in forum: C Programming
    Replies:
    5
    Views:
    327
    James Kuyper
    Nov 11, 2008
  5. mr_guy99493
    Replies:
    0
    Views:
    328
    mr_guy99493
    Feb 4, 2011
Loading...

Share This Page