[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Question about variable length structure

Wen

3/28/2011 10:00:00 AM

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!
2 Answers

Tom St Denis

3/28/2011 11:51:00 AM

0

On Mar 28, 6:00 am, Wen <huangwe...@gmail.com> 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


Ben Bacarisse

3/28/2011 12:27:00 PM

0

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.