472,983 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

nested struct that grow in some cases

Hi,

I have a unique case where I need an array of structs that grows and
within this array is another struct that grows in some cases. I'm
having trouble allocating memory. Since I have never done this before,
I'm sure it's a rookie mistake but I cannot seem to find it. Can
someone render some assistance please?

struct Fpos {
grib_handle *h;
char level[MAX_VAL_LEN];
};

struct Parameter {
double *lvlpress;
double *geomethgt;
double *ab_array;
double *pottemp;
int nlev;
size_t size;
char parameter[MAX_VAL_LEN];
unsigned char **GribNameValues;
struct Fpos **filepos;
} **Darray;
Darray = (struct Parameter **)realloc(Darray,(num + 1)*sizeof(struct
Parameter *));
/* allocate memory for one struct Parameter */
Darray[num] = (struct Parameter *)smalloc(sizeof(struct Parameter));
/* same as above but for the nested struct */
Darrray[num]->filepos = (struct Fpos **)realloc(filepos,(lvlnum +
1)*sizeof(struct Fpos *));
Darrray[num]->filepos[lvlnum] = (struct Fpos *)smalloc(sizeof(struct
Fpos));

When I compile, I get an error saying that filepos is not declared and
is new.

How should this be done properly?

/M

Oct 20 '08 #1
4 2712
When I compile, I get an error saying that filepos is not declared and
is new.

How should this be done properly?
You need to post more code. You don't declare Darray at all, you only set
Darray as a tag.
So Darray = (struct ..... line should be telling you that Darray is not
declared.
Filepos is not declared either:
Darrray[num]->filepos = (struct Fpos **)realloc(filepos, <--- not declared
variable

post your code properly not just cut lines
Oct 20 '08 #2
Sheldon <sh******@gmail.comwrites:
I have a unique case where I need an array of structs that grows and
within this array is another struct that grows in some cases. I'm
having trouble allocating memory. Since I have never done this before,
I'm sure it's a rookie mistake but I cannot seem to find it. Can
someone render some assistance please?
You have a few typos. Did you post actual code?
struct Fpos {
grib_handle *h;
char level[MAX_VAL_LEN];
};

struct Parameter {
double *lvlpress;
double *geomethgt;
double *ab_array;
double *pottemp;
int nlev;
size_t size;
char parameter[MAX_VAL_LEN];
unsigned char **GribNameValues;
struct Fpos **filepos;
} **Darray;
Darray = (struct Parameter **)realloc(Darray,(num + 1)*sizeof(struct
Parameter *));
Beware. This is wrong unless Darray has been set to NULL (or points
previously allocated memory).

Life will be simpler is you use the comp.lang.c standard idiom:

ptr = malloc(how_many * sizeof *ptr);
/* allocate memory for one struct Parameter */
Darray[num] = (struct Parameter *)smalloc(sizeof(struct Parameter));
/* same as above but for the nested struct */
Darrray[num]->filepos = (struct Fpos **)realloc(filepos,(lvlnum +
1)*sizeof(struct Fpos *));
Both Darrray (note three 'r's) and filepos (the one after realloc) are
new undeclared names here. One is a typo, but the other is just
wrong. You probably meant ... realloc(Darray[num]->filepos, ...
Darrray[num]->filepos[lvlnum] = (struct Fpos *)smalloc(sizeof(struct
Fpos));
Three 'r's again and smalloc is an odd name.
When I compile, I get an error saying that filepos is not declared and
is new.

How should this be done properly?
Do you really need pointers of arrays of pointers for both Darray and
the internal filepos? Dynamic arrays are often just represented by
pointers to the first element.

--
Ben.
Oct 20 '08 #3
"MisterE" <Mi*****@nimga.comwrites:
You need to post more code. You don't declare Darray at all, you only set
Darray as a tag.
No, he declared it. The struct tag is "Parameter".

--
Ben.
Oct 20 '08 #4
On Mon, 20 Oct 2008 03:18:29 -0700 (PDT), Sheldon <sh******@gmail.com>
wrote:
Hi,

I have a unique case where I need an array of structs that grows and
within this array is another struct that grows in some cases. I'm
Not actually within. I'd say under or subordinate to each element.
having trouble allocating memory. Since I have never done this before,
I'm sure it's a rookie mistake but I cannot seem to find it. Can
someone render some assistance please?

struct Fpos {
grib_handle *h;
char level[MAX_VAL_LEN];
};

struct Parameter {
double *lvlpress;
double *geomethgt;
double *ab_array;
double *pottemp;
int nlev;
size_t size;
char parameter[MAX_VAL_LEN];
unsigned char **GribNameValues;
struct Fpos **filepos;
} **Darray;
Darray = (struct Parameter **)realloc(Darray,(num + 1)*sizeof(struct
Parameter *));
/* allocate memory for one struct Parameter */
Darray[num] = (struct Parameter *)smalloc(sizeof(struct Parameter));
These are valid assuming that Darray is initialized to a null pointer
and num to zero, which will happen automatically if they are defined
at file scope (or otherwise with static duration). But you should
check for allocation failure and handle it in some reasonable fashion;
to do that it may be better to save realloc's return in a temporary
and check it first before putting it into Darray. And you don't need
to cast the return of malloc et al., if they are properly declared by
#include'ing <stdlib.h>, which they must be.

However, if the number of struct Parameter pointers gets large, on
most implementations it will be inefficient to realloc up by 1 each
time, because it will at least sometimes have to copy quadratically
increasing amounts of data. You might want to do something a little
more complicated like:
unsigned long numalloc /* = 0 *(/, numused /* = 0 */;
struct Parameter ** Darray /* = NULL */;
....
if( numused == /* or >= to be defensive */ numalloc ){
struct Parameter **temp = realloc (Darray,
(numalloc=numalloc*3/2+1) * sizeof (struct Parameter *) );
/* or sizeof *temp, which is equivalent and safe
and easier to maintain, hence clc-preferred */
/* personally I usually start with a smallish nonzero number,
and grow by *2, but that's a little bit more to write */
if( temp == NULL ) HANDLE ERROR;
Darray = temp; /* successfully grew */
}
/* now there's at least Darray[0..numused] and often more */
Darray[numused] = smalloc (sizeof *Darray[numused]);
/* unless smalloc() already handled allocation failure: */
if( Darray[numused] == NULL ) HANDLE ERROR;
/* same as above but for the nested struct */
Darrray[num]->filepos = (struct Fpos **)realloc(filepos,(lvlnum +
1)*sizeof(struct Fpos *));
Darrray[num]->filepos[lvlnum] = (struct Fpos *)smalloc(sizeof(struct
Fpos));
As others have noted, the second filepos here is a variable not a
struct member. But if you make it the struct member that might not be
right either. Did smalloc() initialize the space containing it
suitably? If not, trying to realloc it is Undefined Behavior.
Note that initializing the space to all-zero-bits isn't guaranteed to
make the pointer null, although on _most_ systems it does work.
And smalloc() sounds like a general-purpose routine that doesn't know
about the contents of what it allocates and in particular where
pointers are (or can be).

Plus, your lvlnum is questionable. It can be a separate single
variable only if all Darray[i] elements have the same 'size' of
Darray[i]->filepos at all times, and your code as shown doesn't do
that. If you want say Darray[1]->filepos to grow separately from
Darray[0]->filepos, you need a separate counter in (or mapped
one-to-one to) each struct Parameter. You do have .nlev already, whose
name sounds like it might be intended for this. It similarly must be
initialized to zero, but for an integer type (not a pointer)
initializing to all-bits-zero (if smalloc() does that) is OK.

If you want them all to grow in sync, you need different logic. E.g.:
- allocate Darray to M pointers and each of those M pointers to a
struct Parameter with .filepos null (empty); you can do this using
one-at-a-time logic like the above, or all (or batches) at once
- (then) allocate each Darray [0..M-1] ->filepos to N pointers and set
each of those Darray [0..M-1] ->filepos [0..N-1] to a struct Fpos.
Again you can do these one-at-a-time, in which case you can nest the
loops either way (for each filepos within in Darray or vice versa), or
batched in which case the allocations of the pointer space must loop
over Darray[] (first) but the elements can then nest either way but
can be merged with the pointer allocation only if Darray-major.

Other logics are possible. You'd have to explain more about what you
want to do with (i.e. put into) the resulting data structure.
When I compile, I get an error saying that filepos is not declared and
is new.

How should this be done properly?

/M
- formerly david.thompson1 || achar(64) || worldnet.att.net
Nov 3 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matthew Barnes | last post by:
I was wondering if there would be any interest in extending the struct.unpack format notation to be able to express groups of data with parenthesis. For example: >>> data =...
3
by: Lionel B | last post by:
Greetings. In a template class for which a template parameter may be another template class, I would like to be have access to the type of the nested template parameter. Hopefully the following...
6
by: jwvai316 | last post by:
I don't really know how to say it so I just say it a nested linklist. How do you make LinkLists inside LinkList? Can anyone help me for this? I think an example program will help me a lot. ...
2
by: symbol | last post by:
I am having this problem in a managed c++ DLL which mixes managed and unmanaged C/C++ code. I tried to assign value to a struct array nested in another struct. but I can only write to the first...
1
by: symbol | last post by:
I am having this problem in a managed c++ DLL which mixes managed and unmanaged C/C++ code. I tried to assign value to a struct array nested in another struct. but I can only write to the first...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
6
by: Computer Wizard | last post by:
Hello, I am really scrwed up with the following stuff: struct employee { char* employee_name; }; struct department {
8
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...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.