Passing a double pointer to a function as reference - c -
i'm having hard times trying pass reference of double pointer function. have this:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define max_rows 2 #define max_cols 2 int main(void){ int i=0,lenght=0,maxcolumns=0,j=0,k=0,maxrows=0,maxcolumns2=0; int **columnvect; /*lengthofptr gives me columns need. */ if((lenght=(lengthofptr(ptrmessage)))<=3){ maxcolumns=1; maxcolumns2=2; }else{ maxcolumns=lenght/2; maxcolumns2=maxcolumns; } /* allocating memory double pointer. */ columnvect=malloc(maxcolumns2 * sizeof(int*)); if(columnvect == null){ fprintf(stderr, "memory error.\n"); exit(0); } for(i = 0; < maxcolumns2; i++){ columnvect[i] = malloc(maxrows * sizeof(int)); if(columnvect[i] == null){ fprintf(stderr, "memory error.\n"); exit(0); } } // fills columnvect[i][j] /* passing double pointer messagevector */ messagevector(&columnvect,maxcolumns); return 0; } int messagevector(int ***columnvect,int maxcolumns){ /* allocating memory triple pointer. */ columnvect=(int ***)malloc(sizeof(int **)); //do here . . . return messagevector; }
if run program gives me: (lldb)
in:
3 start libdyld.dylib`start: 0x7fff88b447e0: nop
anyone tell me how in correct way? thanks!
ok, although not quite understand trying accomplish messagevector
function, think question legitimate one, i'll try give insight.
first of all, there issue code have presented in main
function. if malloc
fails while you're allocating maxcolumns2
rows, exit without free
ing allocated rows.
please note have deliberately swapped wording of maxcolumns2
, maxrows
, because in general context bit more sense. conceptually, start allocating rows of int
pointers, , each row allocate columns of int
.
so, may try instead...
/* allocating memory double pointer. */ columnvect = malloc( maxrows * sizeof(int *) ); if ( columnvect == null ){ fputs( "memory error.\n", stderr ); exit(0); } (int i=0; < maxrows; i++) { columnvect[i] = malloc( maxcolumns2 * sizeof(int) ); if ( columnvect[i] == null ) { fputs( "memory error.\n", stderr ); (int j = i-1; j > -1; j--) free( columnvect[j] ); free( columnvect ); columnvect = null; exit(0); } }
the inner loop (using j
counter) walks last allocated row towards 1st one, free
ing them on way. after that, columnsvect
gets free
'ed (that is, memory reserved int
pointers before outer loop) , it's set null
.
i'm nitpicking here, since modern operating systems free memory allocated program after terminates, it's habit free memory have allocated. 1 thing, helps when debugging code. protects creating memory leaks in future, if need embed current code larger project (a library example).
now, regarding passing reference of double pointer function, nice , simple example imho function frees memory reserved pointer, , setting pointer null
. along these lines:
void free_int2d( int ***int2d, int nrows ) { int i; if ( nrows < 1 ) { printf( "*** warning: %s() failed, no memory freed\n", __func__ ); return; } if ( !int2d || !*int2d ) return; (i=0; < nrows; i++) { if ( (*int2d)[i] ) free( (*int2d)[i] ); } *int2d = null; }
hopefully self-explanatory, in case let me point out few things.
the main 1 write *int2d
whenever want express original double pointer inside function (that is, rows of int
pointers).
the other 1 when want express i
'th row, write (*int2d)[i]
. have explicitly enclose 1st dereference inside parenthesis... 2nd dereference done implicitly via [ ] notation.
finally, example of passing reference double pointer function, here inner loop of original code, rewritten using free_int2
function...
for (int i=0; < maxrows; i++) { columnvect[i] = malloc( maxcolumns2 * sizeof(int) ); if ( columnvect[i] == null ) { fputs( "memory error.\n", stderr ); free_int2d( &columnvect, ); exit(0); } }
it substantially simplified, more readable. free_2d( &columnvect, maxrows )
should called on successful termination of program (for reasons explained above).
edit
for performance reasons, may consider pre-allocating buffer malloc
ing columns, , reallocate needed (perhaps doubling size). yields bit more complicated code.
Comments
Post a Comment