## mingw-users

 RE: [Mingw-users] malloc From: Henning, Brian - 2002-09-06 14:06:56 ```my basic question is why can't i do this? double **matrix = (double **) malloc(sizeof(double) * 2); matrix[0][0] = 1.0; matrix[0][1] = 2.0; matrix[1][0] = 3.0; matrix[1][1] = 4.0; -----Original Message----- From: Henning, Brian Sent: Friday, September 06, 2002 9:00 AM To: (E-mail) Subject: [Mingw-users] malloc Hello, can someone tell me how to allocate memory for the 2d array matrix? i am not sure how i can go about doing it. thanks, brian void transpose( double **, int ); int main( int argc, char *argv[] ) { double **matrix; /*need to malloc a 2d array (3x3) here before passing it to transpose */ transpose(matrix, 3); return 0; } /* Transpose of a square matrix, do it in place */ void transpose( double **a, int n ) { int i,j; double tmp; for (i=1;i
 RE: [Mingw-users] malloc From: Henning, Brian - 2002-09-06 14:35:37 ```that makes sense... now i have a new problem.... why is the address the same for the following variables... they should be different double **matrix = (double **) malloc(sizeof(double) * 2 * 2); printf("%d\n", &matrix[0][0]); printf("%d\n", &matrix[1][0]); -----Original Message----- From: David C. Hoos [mailto:david.c.hoos.sr@...] Sent: Friday, September 06, 2002 9:17 AM To: Henning, Brian; (E-mail) Subject: Re: [Mingw-users] malloc ----- Original Message ----- From: "Henning, Brian" To: " (E-mail)" Sent: Friday, September 06, 2002 9:05 AM Subject: RE: [Mingw-users] malloc > my basic question is why can't i do this? > > double **matrix = (double **) malloc(sizeof(double) * 2); > because you need to allocate space for the _square of the dimension i.e., for a 2 x 2 matrix: double **matrix = (double **) malloc(sizeof(double) * 2 * 2); and fore a 3 x 3 matrix: double **matrix = (double **) malloc(sizeof(double) * 3 * 3); ```
 Re: [Mingw-users] malloc From: Luke Dunstan - 2002-09-06 15:27:49 ```The addresses are the same because it is incorrect. If you want to use a pointer-to-pointer you need: double **matrix = (double **)malloc(sizeof(double *) * 2); matrix[0] = (double *)malloc(sizeof(double) * 2); matrix[1] = (double *)malloc(sizeof(double) * 2); matrix[0][0] = 1.0; ... You'll want to use a for() loop to allocate a larger matrix in most cases. If you want a single block like David was trying to explain: double *matrix = (double *)malloc(sizeof(double) * 2 * 2); matrix[2 * 0 + 0] = 1.0; matrix[2 * 0 + 1] = 2.0; matrix[2 * 1 + 0] = 3.0; ... Luke Dunstan ----- Original Message ----- From: "Henning, Brian" To: "'David C. Hoos'" ; " (E-mail)" Sent: Friday, September 06, 2002 10:34 PM Subject: RE: [Mingw-users] malloc > that makes sense... now i have a new problem.... why is the address the same > for the following variables... > they should be different > > double **matrix = (double **) malloc(sizeof(double) * 2 * 2); > printf("%d\n", &matrix[0][0]); > printf("%d\n", &matrix[1][0]); > > > -----Original Message----- > From: David C. Hoos [mailto:david.c.hoos.sr@...] > Sent: Friday, September 06, 2002 9:17 AM > To: Henning, Brian; (E-mail) > Subject: Re: [Mingw-users] malloc > > > > ----- Original Message ----- > From: "Henning, Brian" > To: " (E-mail)" > Sent: Friday, September 06, 2002 9:05 AM > Subject: RE: [Mingw-users] malloc > > > > my basic question is why can't i do this? > > > > double **matrix = (double **) malloc(sizeof(double) * 2); > > > because you need to allocate space for the _square of the dimension > i.e., for a 2 x 2 matrix: > double **matrix = (double **) malloc(sizeof(double) * 2 * 2); > > and fore a 3 x 3 matrix: > > double **matrix = (double **) malloc(sizeof(double) * 3 * 3); > ```
 Re: [Mingw-users] malloc From: David C. Hoos - 2002-09-06 14:16:58 ```----- Original Message ----- From: "Henning, Brian" To: " (E-mail)" Sent: Friday, September 06, 2002 9:05 AM Subject: RE: [Mingw-users] malloc > my basic question is why can't i do this? > > double **matrix = (double **) malloc(sizeof(double) * 2); > because you need to allocate space for the _square of the dimension i.e., for a 2 x 2 matrix: double **matrix = (double **) malloc(sizeof(double) * 2 * 2); and fore a 3 x 3 matrix: double **matrix = (double **) malloc(sizeof(double) * 3 * 3); ```