Home > Back-end >  About dynamic allocation of three dimensional array error assignment problem
About dynamic allocation of three dimensional array error assignment problem

Time:09-24

I wrote two versions of the dynamically allocated three dimensional array of c + + code, the code can open 3 d array to, but a three-dimensional array assignment will appear memory error, error for "Program a received signal SIGSEGV, Segment fault", in addition to the codeblocks 16.01 I use, the compiler for the GCC,
Really can not find the reason where, still ask everybody to help, the code is as follows:
Void * * * alloc3 (int n1, int n2, int n3, int size)
{
Int i3, i2;
Void * * * p;

If ((p=(void * * *) malloc sizeof (n3 * * *) (void))==NULL)
return NULL;
If ((p [0]=(void * *) malloc (n2 n3 * * sizeof (void *)))==NULL) {
free(p);
return NULL;
}
If ((p [0] [0]=(void *) malloc (n1 n2 n3 * * * size))==NULL) {
Free (p [0]).
free(p);
return NULL;
}

For (i3=0; I3 & lt; N3. I3 + +) {
P=p (i3) [0] + n2 * i3;
For (i2=0; I2 & lt; N2. I2 + +)
P [i3] [i2]=p (char *) [0] [0] n1 + size * * * i3 (i2 + n2);
}
Return the p;
}

Void * * * alloc3 (int n_s, int n_row, int n_col, int size)
{
Void * * * p=(void * * *) malloc sizeof (n_s * * *) (void);
Int I, j, k;
If (p==NULL) return NULL;
for(i=0; IP=(void * *) [I] malloc (n_row * sizeof (void *));
For (j=0; J{
[I] [j]=p (char *) malloc (n_col * size);
If (p [I] [j]==NULL) {free (p [0]); free(p); return NULL; }
}
}
Return the p;
}

CodePudding user response:

You can draw a sketch first, it is not easy to write wrong, you can imagine a hook (p), below the p a horizontal hang the garment, and hung up the n_s hooks, each hook a hanger, each n_row a clip hanger, each clamps hold a string of n_col chili,
Second version feeling about the same, but the free () a little problem,

CodePudding user response:

reference 1st floor u010165006 response:
you can draw a sketch first, it is not easy to write wrong, you can imagine a hook (p), below the p a horizontal hang the garment, and hung up the n_s hooks, each hook a hanger, each n_row a clip hanger, each clamps hold a string of n_col chili,
Second version feeling about the same, but the free () a little question,
well, your proposal is very good, but in fact the two versions when testing an array a assignment will debug memory problems, I really don't know where the error is, I use other machines under the codeblocks compilation is also the same problem, and you're free () problem, trouble you can explain how free? I am a rookie, c + + USES matlab at ordinary times

CodePudding user response:

You 2 kinds of writing, malloc (1 + + n_s * n_row n_s) in total, any one failure, to all the free fall, so many times in front of the distribution of your code is not free,
If your program, memory allocation failure, do you want to continue running your program or end? If the end, need not consider free, anyway the end of the process system will clean the unreleased resources,

CodePudding user response:

reference u010165006 reply: 3/f
you 2 kinds of writing, malloc (1 + + n_s * n_row n_s) in total, any one failure, to all the free fall, so many times in front of the distribution of your code is not free,
If your program, memory allocation failure, do you want to continue running your program or end? If the end, you need not consider free, anyway the end of the process system will clean the unreleased resources,
well, I'm going to memory allocation failure ends, but this function when the test problems, through the function allocation of three dimensional array can't assignment, the assignment is a segmentation fault errors, if not the assignment, to print out the index of the matrix: one assignment, will get stuck somewhere,
Double B=* * * * * * (double) alloc3 (n_s n_row, n_col, sizeof (double));
for(int i=0; I{
for(int j=0; J{
for(int k=0; K{
Printf (" % d, % d, % d) ", I, j, k);
//B [I] [j] [k]=1.0;
//printf (" % f ", B [I] [j] [k]);
} printf (" \ n ");
} printf (" -- -- -- -- -- -- -- -- -- -- \ n ");
}
If it is not assignment print directly, also the same problem,
  • Related