473,661 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Yet another nested structure problem (concept phase)

4 New Member
Greetings.

The emulation fever has conquered all kinds of Internet communities, and hence I've decided to write a tool in C which can read and modify a disk image.
(However, I won't tell which kind of disk images until it's finished.

Only problem I have now, where do I start and where do I have to malloc() and where not? It's better to know about this in the concept phase, for when the tool goes beyond the magical 1000-lines extent, it will be near impossible to figure out why the thing GPFs.

Given: disk image, 1024 disk blocks, one block holds 256 bytes of data.
Each byte, word or long word must be accessible directly.

Draft follows.
As you can see, I've decided to be EXTREMELY verbose because there might be other people who sometimes struggle with these large structure concepts.

typedef struct singleBlk {
unsigned char block_data [256];
char * strInLW [256 / 4]; /* string in longword, single-lw */
char * multiStr; /* string that spans several longwords in a seq;
cannot be longer than bytes per block */
} singleBlkS_t;

typedef struct dskImg {
singleBlk_t * dskBlk [1024];
} dskImgS_t;

int main(void)
{
dskImgS_t img; /* allocate mem for 1024 _pointers_ */
dskImgS_t * imgptr = &img; /* ptr to large structure */

buildImgS(imgpt r);
}

void buildImgS (dskImgS_t * pImg)
{
int i;
for (i = 0; i < 1024; i++)
{
/* next line reserves mem for:
1) 1 sub-structure, singleBlk_t, itself
2) 256 bytes of block buffer, block_data
3) 64 char* _pointers_ per block, strInLW
4) 1 char* _pointer_ per block, multiStr
*/

pImg>dskBlk[i] = malloc (256* sizeof (singleBlk_t));

/* now the rest pointed at by ptrs must be allocated too (!) */

for (j = 0; j < 256; j++)
pImg>dskBlk[i]->strInLW[j] = malloc (64 * sizeof(char));

pImg>dskBlk[i]->multiStr = malloc (256 * sizeof(char));
}
}

Phewww...
Now I ask you guys:

Can this be safely simplified a bit, and if so, how?
I think I could get rid of the for() loops, however, this is always dangerous because from my experience, it's damn easy to make mistakes in multiplying the sizeof() things.
Moreover, I'm not quite sure how to write this when I omit the [index].

Well, I'd appreciate any suggestions, criticism, bashing, ... *g*
Nov 1 '08 #1
5 1904
bitstreamer
4 New Member
please, does anyone have any ideas for me? o_O
Nov 2 '08 #2
JosAH
11,448 Recognized Expert MVP
In your description you mention that a single disk block contains 256 bytes only
but in your structure definition it contains 256 bytes and 64 pointers and another
pointer. Why? What are all those char pointers doing there?

kind regards,

Jos
Nov 3 '08 #3
arnaudk
424 Contributor
Just a few additional points
  • Why do you need both img and imgptr? It would be simpler to just use one of them.
  • You can omit the for loops by allocating all the memory in one go, for example: pImg->dskBlk = (singleBlk_t **) malloc (1024*256* sizeof (singleBlk_t));
  • You don't have an explicit declaration of buildImgS() before you use it in main(): some compilers may complain (maybe you've omitted it here for illustrative purposes).
  • There are some typos like ">" instead of "->", your main should have a return value etc. I could quote line numbers if you had used code tags.
Nov 3 '08 #4
Banfa
9,065 Recognized Expert Moderator Expert
In singleBlkS_t assuming that you need all those char arrays (and there is not a good reason why they have to be pointers) then declare the array in the structure rather than a pointer. Then you can just allocate singleBlkS_t without having to have the nested loop allocating all those char arrays as well.

Then in dskImgS_t rather than declaring an array of pointers declare a pointer to an array

singleBlk_t (*dskBlk) [1024];

then just malloc a big single block of memory 1024 * sizeof(singleBl kS_t) long to assign to it.

Doing both these will remove both loops and result in a single call to malloc.

1 issue with that may be that you system does not like the sear size of the block you are trying to allocate, in this case only do the first of my 2 suggestions.
Nov 3 '08 #5
Banfa
9,065 Recognized Expert Moderator Expert
Hmmm ignore my second suggestion I missed the allocating an array of 256 structures to each pointer in the top level structure.

Given that you are allocating over 1GB of data I would probably suggest not allocating it in 1 big chunk but continue allocating 1024 chunks of just over 1MB.
Nov 3 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2397
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
4
4012
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best Regards KInd --
10
3220
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
11
1993
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
8
16890
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
9
7599
by: John Sidney-Woollett | last post by:
Is it possible to use the dblink and dblink_exec features from inside pl/pgsql functions to mimic the behaviour of nested transactions by calling another function or executing some SQL via the dblink (into the same database)? Does the SQL statement executed within the dblink sit in its own isolated transaction/session? And would an error thrown in the dblink statement abort the enclosing session that initiated the call? What is the...
11
1433
by: charlie | last post by:
I recently came across s function (actually many) whose structure is a series of nested ifs with the meat of the function at the very centre: if (condition1) if (condition2) if (condition3) if (condition4) <Body> else { printf("Condition4 failed");
8
5928
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to some functions. I have defined them in the following manner: typedef struct trans Transient; typedef struct sats Satellites;
2
3404
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The results of the analysis are to be stored in a suitable data structure. The main task in this project is to design a suitable ADT (call it WAnalysis) to store the results and enable the following operations to be performed as fast as possible:...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.