c - Allocation of 3-dimensional array -



c - Allocation of 3-dimensional array -

i tried write funtion dynamically allocate 3 dimensional array programme crashes. here code:

#include <stdio.h> #include<stdlib.h> int*** funcao(int a,int b,int c) { int ***ppp=malloc(a*sizeof(int **)); int i,j,k; for(i;i<a;i++) { ppp[i]=malloc(b*sizeof(int *)); } for( i=0;i<a;i++) { for(j=0;j<b;j++) { ppp[i][j]=malloc( c*sizeof(int)); } } for(i=0;i<a;i++) { for(j=0;j<b;j++) { for(k=0;k<c;k++) { ppp[i][j][k]=k*i*j; } } } homecoming ppp; for( i=0;i<a;i++) { for( j=0;j<b;j++) { free(ppp[i][j]); } } for( i=0;i<a;i++) { free(ppp[i]); } free(ppp); } int main() { int ***ppp=funcao(2,5,7); }

do know problem? because free memory in function after homecoming statement?

thanks in advance.

you did not initialize variable i

int i,j,k;

so loop has undefined behaviour

for(i;i<a;i++){ ppp[i]=malloc(b*sizeof(int *)); }

also not clear whar part of function does

for( i=0;i<a;i++){ for( j=0;j<b;j++){ free(ppp[i][j]); }} for( i=0;i<a;i++){ free(ppp[i]); } free(ppp); }

after homecoming statement

return ppp;

you should format code appropriate way. otherwise hard read it.

c multidimensional-array malloc free dynamic-memory-allocation

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -