|
|
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!
|
|
0
|
|
|
|
Reply
|
Wen
|
3/28/2011 10:00:04 AM |
|
On Mar 28, 6:00=A0am, 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
> {
> =A0 HeaderInfo* headerInfo;
> =A0 ContentInfo* contentInfo;
>
> } PacketInfo;
>
> typedef struct
> {
> =A0 UInt8 numNeighbors;
> =A0 NeighborInfo* neighborInfo;
>
> } HeaderInfo;
>
> typedef struct
> {
> =A0 UInt8 ip;
> =A0 UInt8 port;
>
> } NeighborInfo;
>
> typedef struct
> {
> =A0 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 =3D malloc(sizeof PacketInfo) ...
Tom
|
|
0
|
|
|
|
Reply
|
Tom
|
3/28/2011 11:50:43 AM
|
|
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.
|
|
0
|
|
|
|
Reply
|
Ben
|
3/28/2011 12:26:39 PM
|
|
|
2 Replies
259 Views
(page loaded in 0.033 seconds)
Similiar Articles: Some text processing questions - comp.lang.vhdl... in a testbench, I came upon a couple of questions ... If you can set an upper limit on the length of L, there is a hack^wfix: impure function .... variable L: line ... Assignin into Matlab variable struct - comp.soft-sys.matlab ...... Post Question | Groups ... PARAMS={'struct.x',[1 2 3]}; > > for i=1:length ... How to rename fields in structure variable - comp.soft-sys.matlab ... Finding gradient on curve - comp.soft-sys.matlabThe question is: how am i to find the gradient at a particular ... The 'gradient' function allows you to specify variable-length intervals within your two independent ... variable size mode in Embedded Matlab function ? - comp.soft-sys ...... Post Question | ... selecting variables with a particular variable length????? - comp ... variable size mode in Embedded Matlab function ... How to declare variable - comp.soft-sys.sasHi, 2 quick questions. 1. How to declare a variable? For ... or a combination of LENGTH, FORMAT, INFORMAT and LABEL statements. 2) Use the COMPRESS function to ... "undefined function or variable "z"" - comp.soft-sys.matlab ...... Post Question ... function dc=dcsdt(t,c) K=0.8; c0=0.16; vmax=0.36; n=length(t); for i=1:n-1 if t(i ... it says undefined function or variable "z ... eval and fprintf - comp.soft-sys.matlabUndefined function or variable - comp ... eval and fprintf - comp.soft-sys.matlab ... sys.matlab It is usefull when I work with vectors of > different length. See question ... Local array variables in functions - comp.lang.awk... Post Question | Groups ... array, local_array) > { > print length ... How to rename fields in structure variable - comp.soft-sys ... Test vector for only MSB being set. - comp.lang.vhdlI have a function which accepts an unsigned vector of unconstrained length, and assigns it to the variable "work". ... Post Question ... Speed-up the reading of large binary files with complex structures ...... Post Question | Groups ... bt = 10 (in average, since num_bt block has variable length ... Actually, the tmp structure is not an array of ... Data structure/algorithm for variable length record storage and ...I am looking for an algorithm / data structure that works well for large block based devices (eg a mechanical hard drive) which is optimised for insert, get, update ... c++ - Fixed/variable length structure in c# and big endian ...Struct { byte F1[2] SHORT F2 byte F3[512] } BPD CBD { SHORT CLENGTH byte DATA[] } Above are 2 c++ structure. Here SHORT is of 2 byte signed. 7/27/2012 11:21:24 PM
|
|
|
|
|
|
|
|
|