473,586 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can anyone help

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.Matri x[0][0] = 3.2;
NewMatric.Matri x[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.Matri x[0] = (Rowptr *) malloc (sizeof(Rowptr) );
NewMatrix.Matri x[0] = (double *) malloc (4 * sizeof(double)) ;
NewMatrix.Matri x[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
7 1733
"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.Matri x[0][0] = 3.2;
NewMatric.Matri x[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.Matri x[0] = (Rowptr *) malloc (sizeof(Rowptr) );
NewMatrix.Matri x[0] = (double *) malloc (4 * sizeof(double)) ;
NewMatrix.Matri x[0][0] = (double *) malloc (sizeof(double) );
...
...
and many more.
Did your compiler complain about any of these? What type is, in your
opinion,
NewMatrix.Matri x[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
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.Matri x[0][0] = 3.2;
NewMatric.Matri x[0][1] = 6.9;
I assume you meant NewMatrix not NewMatric.

NewMatrix.Matri x 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.Matri x[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.Matri x point to
something useful, possibly like this:

NewMatrix.Matri x = malloc(rows * sizeof(*NewMatr ix.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
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.Matri x[0][0] = 3.2;
NewMatric.Matri x[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.Matri x[0] = (Rowptr *) malloc (sizeof(Rowptr) );
NewMatrix.Matri x[0] = (double *) malloc (4 * sizeof(double)) ;
NewMatrix.Matri x[0][0] = (double *) malloc (sizeof(double) );


Try:

NewMatrix.Matri x[x] = malloc(sizeof(d ouble) * 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
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.Mat rix[0][0] = 3.2;
NewMatric.Mat rix[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.Mat rix[0] = (Rowptr *) malloc (sizeof(Rowptr) );
NewMatrix.Mat rix[0] = (double *) malloc (4 * sizeof(double)) ;
NewMatrix.Mat rix[0][0] = (double *) malloc (sizeof(double) );

Try:

NewMatrix.Matri x[x] = malloc(sizeof(d ouble) * 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
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.Matri x[0][0] = 3.2;
NewMatric.Matri x[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.Matri x[0] = (Rowptr *) malloc (sizeof(Rowptr) );
NewMatrix.Matri x[0] = (double *) malloc (4 * sizeof(double)) ;
NewMatrix.Matri x[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)*size of(double*));
for(i = 0; i < N; i++)
A[i] = malloc((M)*size of(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
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.Matri x[0][0] = 3.2;
NewMatric.Matri x[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.Matri x[0] = (Rowptr *) malloc (sizeof(Rowptr) );
NewMatrix.Matri x[0] = (double *) malloc (4 * sizeof(double)) ;
NewMatrix.Matri x[0][0] = (double *) malloc (sizeof(double) );


Try:

NewMatrix.Matri x[x] = malloc(sizeof(d ouble) * 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
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.Matri x[0][0] = 3.2;
NewMatric.Matri x[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.Matri x[0] = (Rowptr *) malloc (sizeof(Rowptr) );
NewMatrix.Matri x[0] = (double *) malloc (4 * sizeof(double)) ;
NewMatrix.Matri x[0][0] = (double *) malloc (sizeof(double) );


Try:

NewMatrix.Matri x[x] = malloc(sizeof(d ouble) * 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2064
by: J. Nielsen | last post by:
I'm not a programmer and I don't have the time and energy to start learning right now So I need some help. Let's say I have made an HTML Table (consisting of three columns and six rows). In each cell, in the first column I have put in an image (six different ones). In the second column I have merged all six cells into one cell and there put...
14
4620
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like eBay, Yahoo and others. This is what she wants my program to be able to do: (try this test at different websites just for fun). At eBay: -...
6
2081
by: Kartik | last post by:
Hello, I was trying to develop a game in c++ ( text mode ), but I couldn't figure out how to make rest of the elements moving in the game while waiting for the player's input, or, how to accept the player's input in the game. I had visited many sites having c++ tutorials but no one gave enough explanation about this. an anyone please help me?
2
3585
by: pratchaya | last post by:
This is my sample error in my MySQL Log New value of fp=(nil) failed sanity check, terminating stack trace! Please read http://www.mysql.com/doc/en/Using_stack_trace.html and follow instructions on how to resolve the stack trace. Resolved stack trace is much more helpful in diagnosing the problem, so please do resolve it Trying to get...
8
3146
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it. The link to the document is http://www.surveymonkey.com/s.asp?u=689952259313 I have spent 15 hours trying to sort this but to no avail.
6
1248
by: placid | last post by:
Hi all, I'm looking for anyone who is working on a project at the moment that needs help (volunteer). The last project i worked on personally was screen-scraping MySpace profiles (read more at the following link) http://placid.programming.projects.googlepages.com/screen-scrapingmyspaceprofiles but that didn't go all to well, so im...
6
4488
by: theintrepidfox | last post by:
Dear Group I've installed MSSQL 2005 STD on Vista and now can't attach my databases. I've installed SQL SP2 and the SQL Vista Beta Update. The error I'm getting is: Unable to open the physical file <Path to MDF>. Operating system error 5: '''5(Error not found)'''. (Microsoft SQL Server, Error: 5120)
11
1973
by: Ken Fine | last post by:
I am using VS.NET 2008 and like it a lot. One of the very few things I don't like is a bug that seems to spawn literally thousands of   strings, one after the other, on design view changes. Sometimes I will end up with as many as 30,000 of them. I have to do a "Replace" which is slower than I'd like. This is slowing down my work a lot. This...
0
1012
by: canadianbacon | last post by:
Hi, I don't even know if this is the right place to post this but i will, because I need help. I'm trying to finish this basic game. but I am having troubles. If anyone wants to help me out it would be great. I will admit this might take a long time, i am new to java and i suck at it(yes i will also admit that). by anyones standards this...
1
3146
by: sklett | last post by:
I need to bang out a quick application to extract CCITT compressed TIF images from a ton of PDFs. I've used PDFSharp in the past to work with PDFs but ti doesn't have support for the PDF /CCITTFaxDecode filter. I've googled for the obvious terms to try to find some code samples or information about how to accomplish what I want but haven't...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.