Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Question about variable length structure (http://www.velocityreviews.com/forums/t745846-question-about-variable-length-structure.html)

Wen 03-28-2011 10:00 AM

Question about variable length structure
 
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!

Tom St Denis 03-28-2011 11:50 AM

Re: Question about variable length structure
 
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 03-28-2011 12:26 PM

Re: Question about variable length structure
 
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.


All times are GMT. The time now is 05:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.