[Dev-C++] RES: function for allocate memory for 2D vectors
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2007-11-11 13:51:31
|
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 = (int **) malloc(lin*sizeof(int));
if(!vect) return(NULL);
for(i=0; i<col; i++){
vect[i] = (int *) malloc(col*sizeof(int));
if(!vect[i]) return(NULL);
}
return(vect);
}
int main()
{
int **vector2D;
int i, j, lin=3, col=4;
vector2D = Alocate2DVect(lin, col);
if(!vector2D) exit(EXIT_FAILURE);
for(i=0; i<lin; i++)
for(j=0; j<col; j++)
vector2D[i][j] = i + j;
for(i=0; i<lin; i++){
for(j=0; j<col; j++){
printf("%d\t", vector2D[i][j]);
}
printf("\n");
}
for(j=0; j<col; j++)
free(vector2D[col]);
free(vector2D);
system("PAUSE");
return 0;
}
///////// end of code ////////
Is it ok?
Thanks!
André
-----Mensagem original-----
De: Per Westermark [mailto:pw...@ia...]
Enviada em: domingo, 11 de novembro de 2007 04:27
Para: André Macário 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é Macário 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´t?
>
> ////////// code /////////////////////////
>
> int Alocate2DVect(int ***vect, int lin, int col)
> {
> int i, *pont;
>
> *vect = (int **) malloc(lin*sizeof(int)); // line x
> if(!vect) return(0);
> for(i=0; i<col; i++) *(*vect + i) = (int *) malloc(col*sizeof(int));
> if(!vect) return(0); // line y
> return(1);
> }
>
> int main()
> {
> int **vector2D;
> int i, j, lin=3, col=4;
>
> if(!Alocate2DVect(&vector2D, lin, col)) exit(1);
>
> for(i=0; i<lin; i++)
> for(j=0; j<col; j++)
> vector2D[i][j] = i + j;
>
> for(i=0; i<lin; i++){
> for(j=0; 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/2007
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/2007
15:47
|