Re: [Dev-C++] 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 07:26:50
|
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(int)=
);
> 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/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
>
|