Re: [Dev-C++] RES: function for allocate memory for 2D vectors
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Per W. <pw...@ia...> - 2007-11-11 14:18:40
|
As you can see, the code become simler when you return the allocated array. Two things: If you fail to allocate a column, it might be a good idea to clean up any partial allocations before returning NULL. This is important if you intend to continue to run the program after a failed allocation. A 10k x 10x array of 32-bit integers would consume about 400MB, so an allocation failure could potentially leak 400MB memory. Then rename Alocate2DVect to Allocate2DVect and everything should be ok :) /pwm EMA System email: pw...@ia... Per Westermark web: http://iapetus.neab.net Vargvaegen 174B phone: +46 70 214 74 48 S-906 42 Umeaa Sweden On Sun, 11 Nov 2007, Andr=E9 Mac=E1rio Barros wrote: > My way of understand your recommendations > did result in this: > > //////code/////////// > #include <stdio.h> > #include <stdlib.h> > > int **Alocate2DVect(int lin, int col) > { > int i, **vect; > > vect =3D (int **) malloc(lin*sizeof(int)); > if(!vect) return(NULL); > for(i=3D0; i<col; i++){ > vect[i] =3D (int *) malloc(col*sizeof(int)); > if(!vect[i]) return(NULL); > } > return(vect); > } > > int main() > { > int **vector2D; > int i, j, lin=3D3, col=3D4; > > vector2D =3D Alocate2DVect(lin, col); > if(!vector2D) exit(EXIT_FAILURE); > > for(i=3D0; i<lin; i++) > for(j=3D0; j<col; j++) > vector2D[i][j] =3D i + j; > > for(i=3D0; i<lin; i++){ > for(j=3D0; j<col; j++){ > printf("%d\t", vector2D[i][j]); > } > printf("\n"); > } > > for(j=3D0; j<col; j++) > free(vector2D[col]); > free(vector2D); > > system("PAUSE"); > return 0; > } > > ///////// end of code //////// > > Is it ok? > > Thanks! > Andr=E9 > > > -----Mensagem original----- > De: Per Westermark [mailto:pw...@ia...] > Enviada em: domingo, 11 de novembro de 2007 04:27 > Para: Andr=E9 Mac=E1rio Barros > Cc: dev...@li... > Assunto: Re: [Dev-C++] function for allocate memory for 2D vectors > > > First of all - your code would be a bit simpler (you would loose one > indirection) if you modify Allocate2DVect (two l in allocate) to return > the pointer instead of taking it as a parameter. > > Second: your test if (!vect) does not check the result of the allocation, > but if you sent a pointer in to the function. > > Third: You normally don't need to release memory before ending a program, > but if you do, then your code is incorrect. You need to release all > columns before releasing the row vector. > > /pwm > > On Sun, 11 Nov 2007, Andr=E9 Mac=E1rio Barros wrote: > > > Dear Users, > > > > Could you confirm if this function is > > correct? > > The reason of my question is: inspecting > > the address of *vect in line x in the function > > is different of line y. I was expecting equal > > addresses, wasn=B4t? > > > > ////////// code ///////////////////////// > > > > int Alocate2DVect(int ***vect, int lin, int col) > > { > > int i, *pont; > > > > *vect =3D (int **) malloc(lin*sizeof(int)); // line x > > if(!vect) return(0); > > for(i=3D0; i<col; i++) *(*vect + i) =3D (int *) malloc(col*sizeof(in= t)); > > if(!vect) return(0); // line y > > return(1); > > } > > > =09> int main() > > { > > int **vector2D; > > int i, j, lin=3D3, col=3D4; > > > > if(!Alocate2DVect(&vector2D, lin, col)) exit(1); > > > > for(i=3D0; i<lin; i++) > > for(j=3D0; j<col; j++) > > vector2D[i][j] =3D i + j; > > > > for(i=3D0; i<lin; i++){ > > for(j=3D0; j<col; j++){ > > printf("%d\t", vector2D[i][j]); > > } > > printf("\n"); > > } > > > > free(vector2D); > > system("PAUSE"); > > return 0; > > } > > //////////// end of code ///////// > > > > Regards > > Andre > > > > > > No virus found in this outgoing message. > > Checked by AVG Free Edition. > > Version: 7.5.503 / Virus Database: 269.15.28/1123 - Release Date: > 10/11/2007 > > 15:47 > > > > > > -----------------------------------------------------------------------= -- > > This SF.net email is sponsored by: Splunk Inc. > > Still grepping through log files to find problems? Stop. > > Now Search log events and configuration files using AJAX and a browser. > > Download your FREE copy of Splunk now >> http://get.splunk.com/ > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > No virus found in this incoming message. > Checked by AVG Free Edition. > Version: 7.5.503 / Virus Database: 269.15.28/1123 - Release Date: 10/11/2= 007 > 15:47 > > No virus found in this outgoing message. > Checked by AVG Free Edition. > Version: 7.5.503 / Virus Database: 269.15.28/1123 - Release Date: 10/11/2= 007 > 15:47 > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |