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?