c++ - No time reported -


i've got c++ program implements gaussian elimination. compiles , runs ok in calculation part, before it's supposed tell time spent on calculation crashes segfault.

#include <iostream> #include <time.h> #include <omp.h> #include <vector> #include <cstdlib>   using namespace std;  int main() {     const unsigned int n = 10;      // initialize random seed:     srand (time(null));      vector<vector <double> > a(n, vector<double>(n+1)) ;     double buf;     vector<double> x(n);          unsigned int i,j,k;      clock_t t;     t = clock();      double prectime=omp_get_wtime();       //#pragma omp parallel shared() private() num_threads()      //matrix , right-side vector initialisation     for(i = 0; < n; i++)     {         for(j = 0; j < n+1; j++)         {             a[i][j]=(1+rand() % 100)/25.0;             //cout << "a[" << << "][" << j <<"] = " << a[i][j] << endl;         }        }      //there     for(i = 0; < n -1; i++)     {         for(j = + 1; j < n; j++)         {             buf=a[i][i]/a[j][i];             //cout << "buf = " << buf << endl;             (k = 0; k <= n; k++)                             {                     a[j][k] = a[j][k]*buf - a[i][k];                     //cout << "a[" << j << "][" << k <<"] = " << a[j][k] << endl;                 }         }        }      // & again =)     x[n-1] = a[n-1][n]/a[n-1][n-1];     for(i = n-2; >= 0; i--)     {         buf = 0;         (j = i+1; j < n; j++)             {                 buf += a[i][j] * x[j];                 //cout << "buf = " << buf << endl;                       }         x[i]=(a[i][n] - buf)/a[i][i];         cout << "x[" << << "] = " << x[i] << endl;         }      prectime=omp_get_wtime()-prectime;       t=clock()-t;      cout << "the thingy calculated in  " << t << "clicks("<< ((float)t)/clocks_per_sec <<" seconds) " << endl;     cout << "actual time spent " << prectime << "seconds "<< endl;      return 0; } 

it's compiled

g++ -wall -fopenmp 

,but think openmp part can disregarded (it's not used @ stage).

what doing wrong?

edit: if add -d_glibcxx_debug g++ flags used, compiles , runs ok , shows time expected. still doesn't me understand why , went wrong, though.

you falling infinite loop here:

for(i = n-2; >= 0; i--) 

because i unsigned int, when want become -1, overflows (which means indexing wrongly array, since you going out of bounds). result, not reaching point timing should appear. in general, should first sure program correct , measure it's time.

try setting i int, rather unsigned one.


so went wrong?

either program run infinitely, because of infinite loop,

or

it crash, scenario happen, since i take big value (max of unsigned int in system) , access array a invalidly, causing out of bounds access, result in segmentation fault.

by changing i int, allow i negative too, can value -1, making loop discussed in answer ok, since not enter body of loop when i becomes negative.

when dealing unsigned integers , loops decrement counter, aware of danger of overflow! common change type of counters unsigned int, rather int, in order rid of warning:

warning: comparison between signed , unsigned integer expressions

however, should never forget decrementing unsigned integer should used caution!


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -