1. ## Struct Allocation

this thread is the closest I found, so I'll post here although it's years old. If it's preferable to open a new thread, please make it so, or let me know and I'll do it myself. So my problem is as follows:

I have an algorithm that uses a struct many times. It's inneficient to allocate and kill inside the loop, so I wanted a couple of functions to allocate and free the complicated structure; why is it complicated? well because the struct has pointers to double inside, instead of the actual memory. this is good cause the same structure can point to repeated double stuff ELSEWERE. The bottom line, is I need to work with these chained structs:
Code:
typedef struct {
void *contstate;
void *discstate;
REAL *rcontstate;
int indexdiscstate;
} contdiscstate;

typedef struct {
int option;
REAL t;
REAL *delta;
REAL *u;
int indexdiscstate;
} cddiscstate_d;
so what I need, is an allocating function like this one:
Code:
void *cdstatealloc_d(const void *params)
{
const cdstaparams_d *pa=params;
int noptions=(*pa).noptions;

contdiscstate *cds=malloc(sizeof(contdiscstate));
(*cds).discstate=malloc(sizeof(cddiscstate_d));
cddiscstate_d *ds=(*cds).discstate;
(*ds).delta=malloc(sizeof(REAL)*noptions);
(*ds).u=malloc(sizeof(REAL)*noptions);

return (void *) cds;
}
I made the function return a pointer so that I wouldn't have to use double pointers, cause later I can do something like:
void *state=statealloc(params);

the freeing function looks like:
Code:
int cdstatefree_d(void *state, const void *params)
{
contdiscstate *cds=state;
cddiscstate_d *ds=(*cds).discstate;
free((*ds).delta);
free((*ds).u);
free((*cds).discstate);
free(cds);

return 0;
}
this last one gives me a segmentation fault at "free((*ds).delta);" I thought since the pointer is stored deep inside, that it would remember what the memory is and be able to let it go, but it seems that somehow (*ds).delta is not the pointer I need to free the mem I allocated in the other function.

I understand how convoluted this looks, but the program is complicated, and I have given quite a bit of thought to this nested structures and so, there is no simple way to get rid of that. If someone could help me with this, I would greatly appreciate it.

2. Piggybacking on old threads is generally considered bad form around here. Next time, if there's no active thread, start a new one.

Also use code tags when posting source code. A lot of people won't read it if you don't.

For one thing I don't understand why you're using the rather complex (*Struct).Element notation when it's far simpler and a lot more readable to use Struct->Element.

For another pointers inside structs are no problem. Just be sure to free them before you free the struct.

Then there's the question of why you would return a void pointer after allocating contdiskstate... why not just have it directly return a pointer to the struct?

3. I return the void, because the algorithm that uses the struct does not know the contdiscstate struct; it works with void pointers and their size, so that I can later create a different struct and the algorithm will work the same.

You are right about not using ->, I have 20 files and I have to play around with my text editor to change it using regex; it's something I have been postponing;

I do free the inside stuff first, as in here:

Code:
free(ds->delta);
free(ds->u);
free(cds->discstate);
free(cds);
do you think the bug is somewhere else?

4. Originally Posted by nixahn
I return the void, because the algorithm that uses the struct does not know the contdiscstate struct; it works with void pointers and their size, so that I can later create a different struct and the algorithm will work the same.

You are right about not using ->, I have 20 files and I have to play around with my text editor to change it using regex; it's something I have been postponing;

I do free the inside stuff first, as in here:

Code:
free(ds->delta);
free(ds->u);
free(cds->discstate);
free(cds);
do you think the bug is somewhere else?
You probably should get the app running in a debugging utility and see if you can spot it. Your code is a bit, ummm, odd, but I don't think there is a direct problem. The only thing and it's not in evidence here is that when you're working on structs with void pointers is that you have to typecast rather a lot... so that may also be something to investigate.

5. thanks, as you said the problem was somewhere else. I understand my code is strange, but it's a long story. The basic idea is that using void allows me to build algorithms that work for different models, where a model is a number of structures and functions that work with those structures; As I said, thanks for your help.

6. Originally Posted by nixahn
thanks, as you said the problem was somewhere else. I understand my code is strange, but it's a long story. The basic idea is that using void allows me to build algorithms that work for different models, where a model is a number of structures and functions that work with those structures; As I said, thanks for your help.
No worries.