By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

c struct help

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Robert Rota <se*****@csfrontiers.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.physik.fu-berlin.de/~toerring
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.