Ben Bacarisse
3/28/2011 12:27:00 PM
Wen <huangwen77@gmail.com> 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.