Question about variable length structure

W

Wen

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!
 
T

Tom St Denis

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
 
B

Ben Bacarisse

Wen said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top