c struct help

R

Robert Rota

Hello,

Please help me with the following struct development:

I need three levels of a data structure simulating a 3 level
pagetable. I need a structure which has a pointer to an array of
pointers. The array size will be inputted as a command line arg. These
pointers will point to another structure of the same type. Therefore
we will have a pointer to an array of ptrs each pointing to an array
which is of a different size then the first and given as another arg.
Then all of the second level ptrs must point to the same structure but
this structure has a pointer to an array (of size given as arg) of
ints. This is the last level. i think union can be used to
differentiate between the two array types? So, how do I design this
structure? How do I allocate the three levels of arrays of two types
of different sizes at runtime? I can't create all of them initially
because some of the 2nd and 3rd level array elements will not be
needed so I have to allocate them only as needed. I will create the
first level array though.

Here is my lame attempt:

typedef struct {
int level;
union PageEntry {
PAGETABLE ptrs[];
int frames[];
}
} PAGETABLE;

Any help would be greatly appreciated.

Regards
 
J

Jens.Toerring

Robert Rota said:
I need three levels of a data structure simulating a 3 level
pagetable. I need a structure which has a pointer to an array of
pointers. The array size will be inputted as a command line arg. These
pointers will point to another structure of the same type. Therefore
we will have a pointer to an array of ptrs each pointing to an array
which is of a different size then the first and given as another arg.
Then all of the second level ptrs must point to the same structure but
this structure has a pointer to an array (of size given as arg) of
ints. This is the last level. i think union can be used to
differentiate between the two array types? So, how do I design this
structure? How do I allocate the three levels of arrays of two types
of different sizes at runtime? I can't create all of them initially
because some of the 2nd and 3rd level array elements will not be
needed so I have to allocate them only as needed. I will create the
first level array though.
Here is my lame attempt:
typedef struct {
int level;
union PageEntry {
PAGETABLE ptrs[];
int frames[];
}
} PAGETABLE;

If I understand you're description correctly you need variables of
three types in your union - for the first level you need a pointer
to (an array of) pointers to PAGETABLE, on the next level a pointer
to PAGETABLE (which points to the first element of the array of
PAGETABLEs) and on the third a pointer to int. I shortened the
names you're using a bit:

typedef struct {
int level;
union {
struct PAGETABLE **L1;
struct PAGETABLE *L2;
int *frames;
} PE;
} PAGETABLE;


PAGETABLE pt;
int i, j;

pt.level = 1;
pt.PE.L1 = malloc( size1 * sizeof *pt.PE.L1 );

for ( i = 0; i < size1; i++ )
{
pt.PE.L1[ i ].level = 2;
pt.PE.L1[ i ].PE.L2 = malloc( size2 * sizeof *pt.PE.L1[ i ].PE.L2 );

for ( j = 0; j < size2; j++ )
{
pt.PE.L1[ i ].PE.L2[ j ].level = 3;
pt.PE.L1[ i ].PE.L2[ j ].PE.frames =
malloc( size3 * sizeof *pt.PE.L1[ i ].PE.L2[ j ].PE.frames );
}
}

Of course, you should check if malloc() did return successfully on
each step. If everything works out you can use the L1 member of the
union when you're on level 1, the L2 member when you're on level 2
and the frames member on level 3.
Regards, Jens
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,303
Messages
2,571,557
Members
48,359
Latest member
Raqib_121635
Top