473,378 Members | 1,426 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,378 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 2743
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...

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.