Thread: memory allocation of the defined structure having defined structure array.

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    4

    memory allocation of the defined structure having defined structure array.

    I have a question about dynamic memory allocation problem.

    I defined two structures as follow
    Code:
    typedef struct _vec_2D{
       double  xx;
       double  yy;
    }vec_2D;
    
    typedef struct _ptl{
       double  xx;
       double  yy;
       vec_2D **arm_vec;
    }ptl;
    The structure "vec_2D" is the structure having two double variables, and the structure "ptl" is the structure having two double variables and the array of "vec_2D" whose size is 3.

    While I code the program, I memory allocate the array of the "ptl", "ptl_list" in this way.
    Code:
    ptl_list = (ptl **)calloc(N_ptl, sizeof(ptl *));
    for(i_ptl=0; i_ptl<N_ptl; i_ptl++){
       ptl_list[i_ptl] = (ptl *)malloc(sizeof(ptl));
       ptl_list[i_ptl]->arm_vec = (vec_2D **)malloc(3*sizeof(vec_2D *));
       for(i_arm=0; i_arm<3; i_arm++){
           ptl_list[i_ptl]->arm_vec[i_arm] = (vec_2D *)malloc(sizeof(vec_2D));
       }
    
    }
    When I execute the program, the program runs well in the main function.
    However, when the program ends, segmentation fault appear.
    It seems that the memory allocating method I used has the problem.
    Is there any problem in the allocating code that I used?
    If so, can you tell me how to correct the code?

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by montecarlo View Post

    When I execute the program, the program runs well in the main function.
    However, when the program ends, segmentation fault appear.
    It sounds like the problem is how you're freeing the memory ?...

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Your allocation is strangely complex and wasteful. Why aren't you just doing this:
    Code:
    #define ARM_VEC_SIZE 3
    
    typedef struct _vec_2D{
       double  xx;
       double  yy;
    }vec_2D;
    
    typedef struct _ptl{
       double  xx;
       double  yy;
       vec_2D  arm_vec[ARM_VEC_SIZE];
    }ptl;
    
    pt1 *pt1_list = malloc(N_pt1 * sizeof(ptl));
    Or if vec_2D needs to have a variable size (and you just happen to want 3 of them at the moment) then:
    Code:
    typedef struct _ptl{
       double  xx;
       double  yy;
       vec_2D  *arm_vec;
    }ptl;
    
    pt1 *pt1_list = malloc(N_pt1 * sizeof(ptl));
    for (i = 0; i < N_pt1; ++i)
        pt1_list->arm_vec = malloc(ARM_VEC_SIZE * sizeof(vec_2D));
    BTW, don't use calloc unless you're taking advantage of its memory-zeroing behavior. Since doubles and pointers are not guaranteed to be properly "zeroed" by calloc, it's not useful in your case. Zero your doubles manually.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    4
    Thanks for your kind answer.
    Your modified code seems to be much better.
    I'll change my code in your way.
    thanks.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    4
    Yes. I found that the segmentation fault was caused by other part of the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare structure name to #defined value
    By ItsAaron in forum C Programming
    Replies: 3
    Last Post: 02-11-2010, 11:15 AM
  2. pre-defined array of strings within a structure
    By jamie85 in forum C Programming
    Replies: 1
    Last Post: 02-06-2006, 04:56 PM
  3. User defined structure
    By Jamina in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 08:18 PM
  4. Structure Memory Allocation
    By skyglin in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 05:23 PM
  5. Replies: 7
    Last Post: 04-13-2003, 10:53 PM