473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

c struct help

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
1 3992
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
2
by: Arne Styve | last post by:
Hi, I have an API written in C that came with some graphics cards we are going to use in a project. I need to write a small application where this API is to be used, and I decided to try out...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
4
by: JR | last post by:
I need some help. I am trying to return a dirent struct location so i can access what the function found in main(). I dont understand pointers very well and think that is were i am getting it...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
4
by: jadeivel756 | last post by:
I BADLY NEED YOUR HELP...... HELP... hOW TO Pass value to a struct type and permanently store the data after youve given the data.The programming language is C. My problem is that as I exit the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.