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;
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,