Can anyone help

J

Jim

Hi people. I was hoping someone could help me as this is driving me up
the wall.

I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:


typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.


There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I get run time memory allocation errors.

I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));
NewMatrix.Matrix[0] = (double *) malloc (4 * sizeof(double));
NewMatrix.Matrix[0][0] = (double *) malloc (sizeof(double));
....
....

and many more.

Does anyone have any suggestions on a way around this while retraining
the same basic structure.

Any advice would be greatly appreciated as this is driving me slowly
insane :)

Thanks in advance

Jim
 
P

Peter Pichler

Jim said:
typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

No, it doesn't. Let's have a look...

typedef double Rowptr[4]; /* Rowptr is an array of 4 doubles */
typedef double *Rowptr[4]; /* Rowptr is an array of 4 pointers to double */
typedef double (*Rowptr)[4]; /* Rowptr is a pointer to an array of 4 doubles
*/

BTW, many regulars here prefer /* */ over //, though most modern compilers
support both.
typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.

There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I get run time memory allocation errors.

I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));
NewMatrix.Matrix[0] = (double *) malloc (4 * sizeof(double));
NewMatrix.Matrix[0][0] = (double *) malloc (sizeof(double));
...
...
and many more.

Did your compiler complain about any of these? What type is, in your
opinion,
NewMatrix.Matrix[0]? Can you assign it, let's say, a Rowptr *? What about
double *?

(I'm not going to start the endless war about casting the return from
malloc - just don't do it, OK? ;-))
Does anyone have any suggestions on a way around this while retraining
the same basic structure.

Any advice would be greatly appreciated as this is driving me slowly
insane :)

Have I mentioned the FAQ? It's located at
http://www.eskimo.com/~scs/C-faq/top.html. Look up especially questions 6.7
and 6.16, though the whole sections 6 and 7 might be quite educational. 6.16
provides the answer you want.

Peter
 
K

Kevin Goodsell

Jim said:
Hi people. I was hoping someone could help me as this is driving me up
the wall.

I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:


typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.


There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I assume you meant NewMatrix not NewMatric.

NewMatrix.Matrix is an uninitialized pointer. It doesn't point to
anything useful yet, and dereferencing it (which is what you are doing)
causes undefined behavior. In fact, merely examining its value causes
undefined behavior, but typical implementations only freak out when you
dereference.
I get run time memory allocation errors.

But you aren't doing any run-time allocation in that code. An access
violation would be expected (on typical desktop implementations).
I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));

You are still dereferencing an uninitialized pointer here. Also, it's
usually recommended to NOT cast malloc's return value. It doesn't do
anything useful, and it can hide errors. (Did you remember to #include
<stdlib.h>?) Consider using the comp.lang.c-approved idiom for malloc:

p = malloc(N * sizeof(*p));

This is less error-prone and more self-maintaining than other idioms.

The first thing you need to do is to make NewMatrix.Matrix point to
something useful, possibly like this:

NewMatrix.Matrix = malloc(rows * sizeof(*NewMatrix.Matrix));

(Note the use of the clc-approved malloc idiom.)

I think that should take care of it (but the multi-dimensional arrays
and type aliases make things a bit confusing, so I wouldn't be surprised
if I'm making a mistake).

-Kevin
 
S

stau

Hi people. I was hoping someone could help me as this is driving me up
the wall.
Hi!


I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:


typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.


There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I get run time memory allocation errors.

I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));
NewMatrix.Matrix[0] = (double *) malloc (4 * sizeof(double));
NewMatrix.Matrix[0][0] = (double *) malloc (sizeof(double));

Try:

NewMatrix.Matrix[x] = malloc(sizeof(double) * NewMatrix.Rows);

Remenber to read the number of rows to NewMatrix.
You can then use NewMatrix[x][y].

I think this is what you pretend, but be warned: I'm a newbie.
bye.
 
K

Kevin Goodsell

stau said:
Hi people. I was hoping someone could help me as this is driving me up
the wall.

Hi!


I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:


typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.


There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I get run time memory allocation errors.

I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));
NewMatrix.Matrix[0] = (double *) malloc (4 * sizeof(double));
NewMatrix.Matrix[0][0] = (double *) malloc (sizeof(double));


Try:

NewMatrix.Matrix[x] = malloc(sizeof(double) * NewMatrix.Rows);

Don't try that. I Don't know what x is, but in any case it invokes
undefined behavior by examining an indeterminate pointer value. It also
attempts to assign a pointer to an array, and the size of the allocated
memory is questionable.

-Kevin
 
E

Espen Myrland

Hi people. I was hoping someone could help me as this is driving me up
the wall.

I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:


typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.


There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I get run time memory allocation errors.

I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));
NewMatrix.Matrix[0] = (double *) malloc (4 * sizeof(double));
NewMatrix.Matrix[0][0] = (double *) malloc (sizeof(double));
...
...

and many more.

Does anyone have any suggestions on a way around this while retraining
the same basic structure.

Any advice would be greatly appreciated as this is driving me slowly
insane :)

Thanks in advance

Jim


You could also start with a simple matrix test and expand to strucures
afterwards. Maybe somthing like this:


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

#define M 1000 /* number of coloumns */
#define N 1000 /* number of rows */

int main (void) {
double **A; /* matrix */
int i,j;

A = malloc((N)*sizeof(double*));
for(i = 0; i < N; i++)
A = malloc((M)*sizeof(double));

for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
(i==j+1) ? (A[j]=1):(A[j]=0);
printf("%d", (int) A[j]);
}
printf("\n");
}

return 0;
}
 
S

stau

hi.

I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:


typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.


There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I get run time memory allocation errors.

I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));
NewMatrix.Matrix[0] = (double *) malloc (4 * sizeof(double));
NewMatrix.Matrix[0][0] = (double *) malloc (sizeof(double));

Try:

NewMatrix.Matrix[x] = malloc(sizeof(double) * NewMatrix.Rows);

Remenber to read the number of rows to NewMatrix.
You can then use NewMatrix[x][y].

I didn't got your declaration right. You should be using an array of
pointers to double. This way you can alocate an arbitrary number of
"rows".
 
B

Barry Schwarz

hi.

I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:


typedef double (*Rowptr)[4]; // Holds rows of 4 doubles

typedef struct {
int Rows;
Rowptr Matrix;
} MatPtr; // Holds number of rows and matricies.


There will always be 4 columns but the number of rows is variable.

Now the problem is, whenever I declare a Matrix and try to assign
individual values as so:

MatPtr NewMatrix;

NewMatric.Matrix[0][0] = 3.2;
NewMatric.Matrix[0][1] = 6.9;

I get run time memory allocation errors.

I can't find a way with malloc (The program must be in C not C++) of
allocating this space in a way the compiler can accept.

I've tried

NewMatrix.Matrix[0] = (Rowptr *) malloc (sizeof(Rowptr));
NewMatrix.Matrix[0] = (double *) malloc (4 * sizeof(double));
NewMatrix.Matrix[0][0] = (double *) malloc (sizeof(double));

Try:

NewMatrix.Matrix[x] = malloc(sizeof(double) * NewMatrix.Rows);

Remenber to read the number of rows to NewMatrix.
You can then use NewMatrix[x][y].

I didn't got your declaration right. You should be using an array of
pointers to double. This way you can alocate an arbitrary number of
"rows".

An array of pointers to double cannot be used to allocate an arbitrary
number of rows. Once the array is defined, you can only allocate as
many rows as the array has elements.

A pointer to pointer to double can be used to allocate a "dynamic
array" of pointers but that is something else.

The OP's declaration (a pointer to an array of double) is quite
adequate for defining an "array" with a dynamic number of rows.


<<Remove the del for email>>
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top