473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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[i][j]);
But I could NOT get what I want to,
something must wrong in my codes.
Would you please help?

Thanks!

Nov 14 '05 #1
5 2537
PCHOME wrote:

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[i][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 */
Nov 14 '05 #2
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!

Nov 14 '05 #3
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.

Nov 14 '05 #4
PCHOME wrote:

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.
Nov 14 '05 #5
PCHOME wrote:

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);
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
7
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
3
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; }...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.