Program received signal SIGSEGV, Segmentation fault.

Hi,

I am trying to write a program that calculates the product of two large, dense matrices. When I use dimensions 200 x 200 for the matrices the program works fine, but as soon as I go to 300 x 300 I get an popup window saying "Assignment2.exe has stopped working..." I ran the debugger and I get this error message: "Program received signal SIGSEGV, Segmentation fault."

I know the obvious culprit is that the size of the matrices is too large, but I'm wondering if there is any way to optimize my program so it can handle larger matrices (without changing the function prototypes, as these are set in stone for the assignment). In MATLAB I am able to handle larger matrix sizes than this. I appreciate your help. Thanks!

Here is my code:
----------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>

using namespace std;

int matrix_multiply(int n1, int n2, int n3, double *a, double *b, double *c);
int matrix_fill_random(int n1, int n2, double *a);
int matrix_print(int n1, int n2, double *a);


int matrix_multiply(int n1, int n2, int n3, double *a, double *b, double *c)
{
    int i;
    int j;
    int k;

    for (i=0;i<n1;i++) {
        for (j=0;j<n3;j++) {
            *(c+(i*n3)+j) = 0;
            for (k=0;k<n2;k++) {

                *(c+(i*n3)+j) += (*(a+(i*n2)+k) * (*(b+(k*n3)+j)));
            }
        }
    }
    return 0;
}

int matrix_fill_random(int n1, int n2, double *a)
{
    int i;
    for (i=0;i<(n1*n2);i++) {
        *(a+i) = rand() % 20001 - 10000;
        *(a+i) /= 10000;
    }
    return 0;
}

int matrix_print(int n1, int n2, double *a)
{
    int i;
    int j;

    cout << "\n" << endl;

    for (i=0;i<n1;i++) {
        for (j=0;j<n2;j++) {
            if (*(a+(i*n2)+j) >= 0) {
                cout << " ";
            }
            cout << *(a+(i*n2)+j) << "\t";
        }
        cout << " " << endl;
    }
    return 0;
}

int main() {

    int numRowsA;
    int numColsA;
    int numColsB;
    int numIterations;
    int i;
    srand((unsigned int)time(0));

    cout << "Please enter in the number of rows for Matrix A: ";
    cin >> numRowsA;
    cout << "Please enter in the number of columns for Matrix A: ";
    cin >> numColsA;
    cout << "Please enter in the number of columns for Matrix B: ";
    cin >> numColsB;
    cout << "Please enter in the number of iterations for repeating the multiplication: ";
    cin >> numIterations;

    double A[numRowsA][numColsA];
    double B[numColsA][numColsB];
    double C[numRowsA][numColsB];

    matrix_fill_random(numRowsA,numColsA,(&A[0][0]));
    matrix_fill_random(numColsA,numColsB,(&B[0][0]));

    clock_t beforeMult;
    clock_t afterMult;
    clock_t ticks;
    float seconds;
    float secondsPerIteration;

    beforeMult = clock();

    for (i=0;i<numIterations;i++){
        matrix_multiply(numRowsA,numColsA,numColsB,(&A[0][0]),(&B[0][0]),(&C[0][0]));
        delete C;
    }

    afterMult = clock();

    ticks = afterMult - beforeMult;
    seconds = (float(ticks))/numIterations;
    secondsPerIteration = seconds/CLOCKS_PER_SEC;

    cout << "The number of total clock ticks is: " << ticks << endl;
    cout << "The number of ticks per multiplication is: " << seconds << endl;
    cout << "The number of seconds per multiplication is: "  << secondsPerIteration << endl;

    delete A;
    delete B;
    delete C;
}
http://codepad.org/YchwecrO
Seems to work, after I got rid of the delete statements. I did not notice where you have the "new" operators in your code. Also, suppose that you modify lines 79 to 81 to use new. On line 96 you delete C, so what is left for you to delete on line 111?
Hi,

Thanks for the suggestion. Using the new operator worked for me, as in the following code:
1
2
3
    double *A = new double[numRowsA*numColsA];
    double *B = new double[numColsA*numColsB];
    double *C = new double[numRowsA*numColsB];

Thanks again!
If you use new[], then you need to delete[] (note the brackets)

By the way, ¿why the obfuscation? ¿you do now that you can write a[i] to access an element?
I know that a[i] accesses an element. The problem was not with accessing elements, but it was with the program shutting down for multiplication of large matrices (n = 300 or more). The problem goes away when I use the new operator, and I just wanted to share this with the forum.
Topic archived. No new replies allowed.