1

What is the best way to declare c++ 2d arrays which can have a less effect from the fixed and limited stack size. I recently got segmentation fault due to stackoverflow in following code. How does vectors tackle this issue?

Here is the code which I developed.

#include <iostream>
#include <stdlib.h>

using namespace std;

void printMatrix(double *mat);
void generateMat(double *mat);
void multiplyMat(double *a,double *b,double *c);

int n;

int start_s;
    // the code you wish to time goes here
int stop_s;


int main(){ 

    for(int i=500;i<1000;i++){

    cout<< "Enter n:";
    //cin >> n;
    n=i;
    cout<< "n="<<n<<endl;

    double a[n][n],b[n][n],c[n][n]; //c = a * b, c is the
    // result matrix

    generateMat(*a);
    generateMat(*b);

    // initializing c matrix with 0s'
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            c[i][j]=0;
        }
    }

    cout<<"Matrix 1"<<endl;
    //printMatrix(*a);

    cout<<endl<<"Matrix 2"<<endl;
    //printMatrix(*b);

    multiplyMat(*a,*b,*c);

    cout<<endl<<"Result Matrix"<<endl;
    //printMatrix(*c);

    cout << endl<<"Execution time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;

    }
    return 0;
}

void multiplyMat(double *a,double *b,double* c){
    start_s=clock();
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            for(int k=0;k<n;k++){
                *(c+i*n+j)+=(*(a+i*n+k)) * (*(b+k*n+j));
            }
        }
    }
    stop_s=clock();
}

void printMatrix(double *mat){
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            cout<< *(mat+i*n+j)<< " ";
        }
        cout<<endl;
    }
}

void generateMat(double *mat){
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++){
            *(mat+i*n+j)=(double)rand()/RAND_MAX*10;
        }
    }
}
Dinuka Salwathura
  • 924
  • 16
  • 33
  • 3
    Can you share code? – Rushikesh Deshpande Jun 10 '17 at 11:27
  • 2
    @DinukaSalwathura: don't comment your own question. **Edit your question** to improve it. Put code there with four spaces before each line. – Basile Starynkevitch Jun 10 '17 at 11:29
  • 4
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Jun 10 '17 at 11:30
  • @RushikeshDeshpande added the code. Thank for the suggestions. – Dinuka Salwathura Jun 10 '17 at 11:31
  • 1
    add error you are getting and do analyse why the error is coming before posting question – Rushikesh Deshpande Jun 10 '17 at 11:35
  • @RushikeshDeshpande thanks – Dinuka Salwathura Jun 10 '17 at 11:36
  • 3
    First, don't use `variable sized array` (not recommendable - all C++ compilers will not allow this). Second, you are declaring the array in main function, so the memory will be placed on stack (as it is not dynamic allocated) which is very limited by default (8 mb on my linux). Increase the stack size and re-run the program and observe the output by debugging. – infinite loop Jun 10 '17 at 12:11

1 Answers1

4

Your matrices are using space on the local call stack (which is often limited to one, or a few, megabytes).

You should consider making a class for them, which uses heap allocated data (and release it in the destructor). This answer is for C, not C++, but should be inspirational (just recode it in genuine C++, perhaps using smart pointers and/or some C++ standard container[s]).

You might represent a matrix with some internal std::vector data, and provide operations (i.e. member functions) to resize that matrix and access or modify its element of indices i & j

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547