How to realloc( ) for M[m][n]

P

PCHOME

Hi!

I need to implement a matrix M which has the form:
double M[m][n];
, but the m and n are only known on RUN time.

I am not good in C,

I am thinking to use:

double **M;
int m, n;

int i = 0, j = 0, max = 100;
double entry[MAX];


M = ( double **) malloc(max * sizeof(double) );

if ( M == ( double **) NULL ) PRINT_ERR;

while (! EOF ) {
// read the first row and put all entries into entry[MAX],
// and decide the value n in RUN time
// n is the num of column in matrix
n = read_a_entry_in_a_row(infile, entry);
// now I have n

if ( n < max )
strcpy( &M[0][0] ,entry);

if ( n >= max )
{ max *=2 ;

M = ( double **) realloc( ( double **)M, max * sizeof(double) );

if ( M == ( double **) NULL ) PRINT_ERR;

strcpy( &M[0][0] ,entry);
}

max = 10 ;
// allocate max*n elements to M
M = ( double **) realloc( ( double **)M, max*n * sizeof(double) );

int cur_row = 1 ;
// Now read the following rows and decide the value m in RUN time
while (! EOF )
{
// read one more row and save all entries in the row
// to M[cur_row][0] - M[cur_row][n-1]
read_one_more_row(infile, &M[cur_row++])
if (cur_row > max )
{
max *= 2 ;
M = ( double **) realloc( ( double **)M, max*n * sizeof(double)
);
}
}

}

m = cur_row - 1 ;
M = ( double **) realloc( ( double **)M, m*n * sizeof(double) );

// print out all entries of Matrix
for (i = 0; i < m ; i++ )
for (j = 0; j < n ; j++ )
printf("M[%d][%d] = %f,", i, j, M[j]);


But I could NOT get what I want to,
something must wrong in my codes.
Would you please help?

Thanks!
 
P

pete

PCHOME said:
Hi!

I need to implement a matrix M which has the form:
double M[m][n];
, but the m and n are only known on RUN time.

I am not good in C,

I am thinking to use:

double **M;
int m, n;

int i = 0, j = 0, max = 100;
double entry[MAX];

M = ( double **) malloc(max * sizeof(double) );

if ( M == ( double **) NULL ) PRINT_ERR;

while (! EOF ) {
// read the first row and put all entries into entry[MAX],
// and decide the value n in RUN time
// n is the num of column in matrix
n = read_a_entry_in_a_row(infile, entry);
// now I have n

if ( n < max )
strcpy( &M[0][0] ,entry);

if ( n >= max )
{ max *=2 ;

M = ( double **) realloc( ( double **)M, max * sizeof(double) );

if ( M == ( double **) NULL ) PRINT_ERR;

strcpy( &M[0][0] ,entry);
}

max = 10 ;
// allocate max*n elements to M
M = ( double **) realloc( ( double **)M, max*n * sizeof(double) );

int cur_row = 1 ;
// Now read the following rows and decide the value m in RUN time
while (! EOF )
{
// read one more row and save all entries in the row
// to M[cur_row][0] - M[cur_row][n-1]
read_one_more_row(infile, &M[cur_row++])
if (cur_row > max )
{
max *= 2 ;
M = ( double **) realloc( ( double **)M, max*n * sizeof(double)
);
}
}

}

m = cur_row - 1 ;
M = ( double **) realloc( ( double **)M, m*n * sizeof(double) );

// print out all entries of Matrix
for (i = 0; i < m ; i++ )
for (j = 0; j < n ; j++ )
printf("M[%d][%d] = %f,", i, j, M[j]);

But I could NOT get what I want to,
something must wrong in my codes.
Would you please help?


/* BEGIN new.c */

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
double **M;
void *old;
size_t a, b, count_a, count_b;

b = 10;
a = 2;
M = malloc(a * sizeof *M);
if (M == NULL) {
fputs("malloc(a * sizeof *M) problem\n", stderr);
exit(EXIT_FAILURE);
}
for (count_a = 0; count_a != a; ++count_a) {
M[count_a] = malloc(b * sizeof *M[count_a]);
if (M[count_a] == NULL) {
fputs("malloc(b * sizeof *M[count_a]) problem\n", stderr);
exit(EXIT_FAILURE);
}
for (count_b = 0; count_b != b; ++count_b) {
M[count_a][count_b] = 10 * count_a + count_b;
}
}
for (count_a = 0; count_a != a; ++count_a) {
for (count_b = 0; count_b != b; ++count_b) {
printf("%f\n", M[count_a][count_b]);
}
}
putchar('\n');
++b;
for (count_a = 0; count_a != a; ++count_a) {
old = M[count_a];
M[count_a] = realloc(old, b * sizeof *M[count_a]);
if (M[count_a] == NULL) {
fputs("malloc(b * sizeof *M[count_a]) problem\n", stderr);
exit(EXIT_FAILURE);
}
M[count_a][b - 1] = 10 * count_a + count_b;
}
for (count_a = 0; count_a != a; ++count_a) {
for (count_b = 0; count_b != b; ++count_b) {
printf("%f\n", M[count_a][count_b]);
}
}
putchar('\n');
++a;
old = M;
M = realloc(old, a * sizeof *M);
if (M != NULL) {
M[a - 1] = malloc(b * sizeof *M[a - 1]);
if (M[a - 1] == NULL) {
fputs("malloc(b * sizeof *M[a - 1]) problem\n", stderr);
exit(EXIT_FAILURE);
}
for (count_b = 0; count_b != b; ++count_b) {
M[a - 1][count_b] = 10 * (a - 1) + count_b;
}
} else {
M = old;
--a;
}
for (count_a = 0; count_a != a; ++count_a) {
for (count_b = 0; count_b != b; ++count_b) {
printf("%f\n", M[count_a][count_b]);
}
}
while (a-- != 0) {
free(M[a]);
}
free(M);
return 0;
}

/* END new.c */
 
P

PCHOME

Thank you so much for your help!
It looks like your code allocates the number of row(int a) first, then
allocates the number of column(int b).

if the input file for the matrix M has the form


a_11, a_12, ... , a_1n
a_21, a_22, ... , a_2n
.... .... .... ....
.... .... .... ....
a_m1, a_m2, ... , a_mn
EOF

the n(number of column) can not be known until the first row is read,
and m(number of row) can not be known until EOF is reached.

Any suggestion?
Thanks!
 
P

PCHOME

BTW,

double **M;
......
M[count_a] = realloc(old, b * sizeof *M[count_a]); <---- *
....
does the line above( the one with <---- * ) equals

M[count_a] = realloc(old, b * sizeof (double));

?

Thanks again.
 
P

pete

PCHOME said:
BTW,

double **M;
.....
M[count_a] = realloc(old, b * sizeof *M[count_a]); <---- *
...
does the line above( the one with <---- * ) equals

M[count_a] = realloc(old, b * sizeof (double));

Yes.
ptr = malloc(n * sizeof *ptr);
is the generally preferred idiom on this newsgroup
for allocation of an array of n elements.
 
P

pete

PCHOME said:
Thank you so much for your help!
It looks like your code allocates the number of row(int a) first, then
allocates the number of column(int b).

if the input file for the matrix M has the form

a_11, a_12, ... , a_1n
a_21, a_22, ... , a_2n
... .... .... ....
... .... .... ....
a_m1, a_m2, ... , a_mn
EOF

the n(number of column) can not be known until the first row is read,
and m(number of row) can not be known until EOF is reached.

Any suggestion?

I think you can follow what I did in the example.
At the end I incremented ++a,
which is equivalent to ++m in your program.
At that time the value for b was known, or
the value for your n would have been known.

++a;
old = M;
M = realloc(old, a * sizeof *M);
if (M != NULL) {
M[a - 1] = malloc(b * sizeof *M[a - 1]);
if (M[a - 1] == NULL) {
fputs("malloc(b * sizeof *M[a - 1]) problem\n", stderr);
exit(EXIT_FAILURE);
}
for (count_b = 0; count_b != b; ++count_b) {
M[a - 1][count_b] = 10 * (a - 1) + count_b;
}
} else {
M = old;
--a;
}


while (a-- != 0) {
free(M[a]);
}
free(M);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top