Struct Allocation

This is a discussion on Struct Allocation within the C Programming forums, part of the General Programming Boards category; this thread is the closest I found, so I'll post here although it's years old. If it's preferable to open ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    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.
    Last edited by Salem; 02-07-2011 at 01:42 AM. Reason: Split thread, fix code tags

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

    Your code seems unnecessarily complex...
    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?
    Last edited by CommonTater; 02-06-2011 at 06:21 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    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. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nixahn View Post
    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. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    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. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nixahn View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cant understand Linked list example:
    By satty in forum C Programming
    Replies: 15
    Last Post: 08-13-2010, 11:12 AM
  2. Error in Exceptions.h file during build of QuickFix library
    By arupsarkar in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 11:30 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-03-2008, 12:10 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21