By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Can anyone help

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Jim" <am*************@hotmail.com> wrote in message
....
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
Nov 14 '05 #2

P: n/a
Jim wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #3

P: n/a
On Mon, 29 Dec 2003 12:28:58 -0800, Jim wrote:
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.
Nov 14 '05 #4

P: n/a
stau wrote:
On Mon, 29 Dec 2003 12:28:58 -0800, Jim wrote:

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #5

P: n/a
am*************@hotmail.com (Jim) writes:
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[i] = malloc((M)*sizeof(double));

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

return 0;
}

Nov 14 '05 #6

P: n/a
hi.

On Mon, 29 Dec 2003 22:08:53 +0000, stau wrote:

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".

Nov 14 '05 #7

P: n/a
On Tue, 30 Dec 2003 09:14:57 +0000, stau <st**@pretogal.pt> wrote:
hi.

On Mon, 29 Dec 2003 22:08:53 +0000, stau wrote:

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

This discussion thread is closed

Replies have been disabled for this discussion.