473,395 Members | 2,006 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,395 software developers and data experts.

Segmentation fault in Matrix Multiplication

Hi Guys,

I have a matrix multiplication program in C that multiplies two
matrices.

When their size is 3*3 or 800*800, the program runs fine. But above
that size, I get a "segmentation fault".

I need this huge size as part of my assignment.

Can anyone tell me what's going wrong?

Thanks.

Jan 26 '06 #1
14 4894
am*******@gmail.com wrote:
Hi Guys,

I have a matrix multiplication program in C that multiplies two
matrices.

When their size is 3*3 or 800*800, the program runs fine. But above
that size, I get a "segmentation fault".

I need this huge size as part of my assignment.

Can anyone tell me what's going wrong?


There's a bug in your program.

It's probably the mis-use of store on line 17.

--
Chris "understanding is a three-edged sword" Dollin
Jan 26 '06 #2
Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

Thanks

Jan 26 '06 #3
am*******@gmail.com wrote:
Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

Thanks


He was following up on your implied suggestion of use of ESP to guess
what you have done. You couldn't have done anything along the lines of
running out of memory and not checking for success in malloc(), or you
would have let us see it.
Jan 26 '06 #4
am*******@gmail.com wrote:
Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.

Thanks
Please quote your reply. If you're using google, search the newsgroup and you
will receive a plethora of hints on hwo to do so.

Add quote: There's a bug in your program.

It's probably the mis-use of store on line 17.


That was his point. How can someone help you without seeng some code. It's
always best if you can post a minimal program that compiles and exhibits the
behavior you describe. In some cases just the function and other releavent
parts would do. Perhaps your code doesn't check memory allocations or writes to
memory outside of what you have allocated.
Jan 26 '06 #5
Im very sorry about that. Don't know what ESP means. Here's the code.

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

#define N 850
#define T 1000
void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N]);
void fillMatrix(double a[N][N]);
void prdoubleMatrix(double a[N][N]);

/* Main Method */
main()
{
printf("Hi");
clock_t now,later;
double passed;

//srand( (unsigned)time( NULL ) ); /* Set
initial seed */
printf("RAND MAX is %d\n",RAND_MAX);

double p[N][N] = { 0 }; /* Intializing p
matrix to 0 */
double q[N][N] = { 0 }; /* Intializing q
matrix to 0 */
double r[N][N]= { 0 }; /* Intializing r
matrix to 0 */

now = clock();
fillMatrix(p); /* Randomly fill
Matrix p */
printf("Hi");
printf("Matrix p \n");
printf("======== \n");
//printMatrix(p); /* Display
Matrix p */

fillMatrix(q); /* Randomly fill
Matrix q */
printf("Matrix q \n");
printf("======== \n");
//printMatrix(q); /* Display
Matrix q */

//now = clock();
multiplyMatrices(p, q, r); /* Multiply p and q and put
results in r */
later = clock();
passed = (double) (later - now) / CLOCKS_PER_SEC;

printf("Time taken for multiplication %0.20lf seconds\n", passed);
printf("Matrix r (Matrix p * Matrix q) \n");
printf("============================== \n");
//printMatrix(r); /* Display
Matrix r */

} /* end of main function */

void fillMatrix(double a[N][N]) /* function to fill a matrix with
random numbers */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
a[i][j] = (double) T * rand() / (RAND_MAX + 1.0);
}
}
}

/* function to multiply two matrices */
void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N])
{
int i, j, k;
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
for(k=0; k<N; k++)
{
result[i][j] = result[i][j] + ( a[i][k] * b[k][j] );
}
}
}
}
void printMatrix(double a[N][N]) /* function to display the contents
of a matrix */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}

Jan 26 '06 #6
am*******@gmail.com wrote:

Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.


That's what we have crystal balls for. It is also the reason there
is no point in including any context in a message, so that the
crystal ball can be given a thorough exercise. Your article is a
marvel of completeness and clarity. Without such things we would
have no guessing to do, and life would be extremely boring. We
might even be able to give accurate answers, which would never do.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>

Jan 26 '06 #7
am*******@gmail.com wrote:
Chris,

Thanks for your reply.

But I was wondering where you saw my code. I didn't post it.


That was the whole point of Chris's post. How can anyone tell you what
is wrong with your code if you don't post it?

Also, please provide context when replying, there is no guarantee that
people will have seen the post you are replying to since Google is only
one of very many ways of accessing Usenet. See
http://cfaj.freeshell.org/google/ for details on how to reply properly
through Google.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 26 '06 #8
On 2006-01-26, am*******@gmail.com <am*******@gmail.com> wrote:
Hi Guys,

I have a matrix multiplication program in C that multiplies two
matrices.

When their size is 3*3 or 800*800, the program runs fine. But above
that size, I get a "segmentation fault".
800*800 is huge - 640000 - that's a lot of memory to be allocating on
the stack [pedants: in an auto variable] and since there is no way in
the standard provided for an implementation to indicate that the
declaration of an auto variable "failed", it's undefined behavior
I need this huge size as part of my assignment.
You might have better luck with malloc().
Can anyone tell me what's going wrong?


Your arrays are too big, and you have three of them.
Jan 26 '06 #9
am*******@gmail.com wrote:
Im very sorry about that. Don't know what ESP means. Here's the code.

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

#define N 850
#define T 1000
void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N]);
void fillMatrix(double a[N][N]);
void prdoubleMatrix(double a[N][N]);

/* Main Method */
main()
{
printf("Hi");
clock_t now,later;
double passed;

//srand( (unsigned)time( NULL ) ); /* Set
initial seed */
printf("RAND MAX is %d\n",RAND_MAX);

double p[N][N] = { 0 }; /* Intializing p
matrix to 0 */
double q[N][N] = { 0 }; /* Intializing q
matrix to 0 */
double r[N][N]= { 0 }; /* Intializing r
matrix to 0 */

now = clock();
fillMatrix(p); /* Randomly fill
Matrix p */
printf("Hi");
printf("Matrix p \n");
printf("======== \n");
//printMatrix(p); /* Display
Matrix p */

fillMatrix(q); /* Randomly fill
Matrix q */
printf("Matrix q \n");
printf("======== \n");
//printMatrix(q); /* Display
Matrix q */

//now = clock();
multiplyMatrices(p, q, r); /* Multiply p and q and put
results in r */
later = clock();
passed = (double) (later - now) / CLOCKS_PER_SEC;

printf("Time taken for multiplication %0.20lf seconds\n", passed);
printf("Matrix r (Matrix p * Matrix q) \n");
printf("============================== \n");
//printMatrix(r); /* Display
Matrix r */

} /* end of main function */

void fillMatrix(double a[N][N]) /* function to fill a matrix with
random numbers */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
a[i][j] = (double) T * rand() / (RAND_MAX + 1.0);
}
}
}

/* function to multiply two matrices */
void multiplyMatrices(double a[N][N], double b[N][N], double
result[N][N])
{
int i, j, k;
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
{
for(k=0; k<N; k++)
{
result[i][j] = result[i][j] + ( a[i][k] * b[k][j] );
}
}
}
}
void printMatrix(double a[N][N]) /* function to display the contents
of a matrix */
{
int i, j;
for (i=0; i<N; i++)
{
for (j=0; j<N; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}

You're not trashing the stack are you? Does you code work if N is smaller?

If 'yes', perhaps try declaring these as 'static', e.g:

static double p[N][N] = { 0 };
static double q[N][N] = { 0 };
static double r[N][N]= { 0 };
--
==============
*Not a pedant*
==============
Jan 26 '06 #10
am*******@gmail.com wrote:
I'm very sorry about that.
About what?
Don't know what ESP means.


ESP stands for Extra-sensory perception.
http://en.wikipedia.org/wiki/Extra-sensory_perception
Jan 26 '06 #11
Was my first posting.Really appreciate all the replies; some gave me
advice on how to post/net etiquette and some on what the problem was.
Thanks everyone.

The problem was my account quota in the department server.

Jan 26 '06 #12
am*******@gmail.com wrote:
Was my first posting.Really appreciate all the replies; some gave me
advice on how to post/net etiquette and some on what the problem was. ^^^^^^^^^^^^^^^^^^^^^^^^^

And what happened to those?
The problem was my account quota in the department server.


What problem?

Cheers

Vladimir

--
Dear Lord:
I just want *one* one-armed manager so I never have to hear "On
the other hand", again.

Jan 26 '06 #13
Ico
am*******@gmail.com wrote:
Was my first posting.Really appreciate all the replies; some gave me
advice on how to post/net etiquette and some on what the problem was.
One more last piece of advice : please quote the message(s) you are
replying to, to provide proper context.
The problem was my account quota in the department server.


This might have triggered your problem, but this should not *be* the
problem. Your program should have detected any shortage on resources and
exited with a proper error message, instead of causing a segmentation
fault. You might want to add proper error handling where appropriate.
--
:wq
^X^Cy^K^X^C^C^C^C
Jan 26 '06 #14
On 26 Jan 2006 06:20:43 -0800, in comp.lang.c , am*******@gmail.com
wrote:
Im very sorry about that. Don't know what ESP means.


telepathy.

Since you originally didn't post any code, how could anyone possibly
help solve your problem except by guessing?

For what its worth, all compilers have limits on how big a chunk of
memory they can grab. Maybe you exceeded that.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 26 '06 #15

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

Similar topics

3
by: eenna | last post by:
Hello there, I've a question regarding segmentation fault. When I run my C++ code on SUN Workshop 6 under UNIX Solaris 8, it compiles correctly, but after execution it gives a segmentation fault!...
6
by: songfire | last post by:
Hi everyone, This isn't so much a c++ problem as it is a linux problem, but I am hoping someone can point me in the right direction. I have a program that requires a moderately large matrix. I...
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
15
by: conor.robinson | last post by:
I'm running operations large arrays of floats, approx 25,000 x 80. Python (scipy) does not seem to come close to using 4GB of wired mem, but segments at around a gig. Everything works fine on...
4
by: John Doe | last post by:
segmentation error !!!! hi guys , i wrote this program to multiply two matrices (just the basic code without checkin 4 the condition n==p ) " #include<stdio.h> main() { int a,b,c; int...
5
by: mprathap | last post by:
Let me state my problem. This is my main.c #include <stdio.h> #include "includes/first_order_local_matrix.h" int main()
5
by: lancer6238 | last post by:
Dear all, I'm trying to implement the Smith-Waterman algorithm for DNA sequence alignment. I'm aligning a 2040 bp query sequence against a 5040 bp sequence. I'll be trying to implement a parallel...
1
by: Dameon99 | last post by:
Hi, I have just spent many hours debugging my code and now when I run it I get the following error: "An access violation (Segmentation fault) raised in your program" I have researched on this...
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.