Matrix multiplication in C++ gives Runtime Error -
i'm using following c++ code matrix multiplication , runs fine size = 500. when size = 600 or above code fails. (runtime error)
i ran on ideone.com ouputs "runtime error time: 0 memory: 3292 signal:11"
and in local machine giving me error
#include <cstdlib> #include<iostream> #include <stdio.h> #include <sys/time.h> using namespace std; class timer { private: timeval starttime; public: void start(){ gettimeofday(&starttime, null); } double stop(){ timeval endtime; long seconds, useconds; double duration; gettimeofday(&endtime, null); seconds = endtime.tv_sec - starttime.tv_sec; useconds = endtime.tv_usec - starttime.tv_usec; duration = seconds + useconds/1000000.0; return duration; } static void printtime(double duration){ printf("%5.6f seconds\n", duration); } }; using namespace std; const int size = 600; // size*size matrix void multiplymatricessequential(double a[][size],double b[][size],double ans[][size]); int i,j,k; double s; /* * */ int main(int argc, char** argv) { double a[size][size], b[size][size], ans[size][size]; // assign numbers matrix , b (i = 0; < size; i++) { (j = 0; j < size; j++) { a[i][j]=(double)rand()/rand_max; b[i][j]=(double)rand()/rand_max; } } multiplymatricessequential(a,b,ans); return 0; } void multiplymatricessequential(double a[][size],double b[][size],double ans[][size]) { timer timer = timer(); timer.start(); (i = 0; < size; i++) { (j = 0; j < size; j++) { (k = 0; k < size; k++) s += a[i][k] * b[k][j]; ans[i][j] = s; s = 0.0; } } double duration = timer.stop(); cout << "sequential method time elapsed size " << size << " : "; timer.printtime(duration); }
so doing wrong here ?
note : still same when timer not used.
#include <cstdlib> #include<iostream> #include <stdio.h> #include <sys/time.h> using namespace std; const int size = 500; // size*size matrix void multiplymatricessequential(double a[][size],double b[][size],double ans[][size]); int i,j,k; double s; /* * */ int main(int argc, char** argv) { double a[size][size], b[size][size], ans[size][size]; // assign numbers matrix , b (i = 0; < size; i++) { (j = 0; j < size; j++) { a[i][j]=(double)rand()/rand_max; b[i][j]=(double)rand()/rand_max; } } multiplymatricessequential(a,b,ans); return 0; } void multiplymatricessequential(double a[][size],double b[][size],double ans[][size]) { (i = 0; < size; i++) { (j = 0; j < size; j++) { (k = 0; k < size; k++) s += a[i][k] * b[k][j]; ans[i][j] = s; s = 0.0; } } }
first of - if you're going read data unknown size - recommend using dynamic memory allocation storing instead of pre-allocating fixed-size arrays. if not case or don't want listen advice better allocate bigger arrays in global namespace instead on stack. means code that:
using namespace std; const int size = 500; // size*size matrix void multiplymatricessequential(double a[][size],double b[][size],double ans[][size]); int i,j,k; double s; /* * */ double g_a[size][size], g_b[size][size], g_ans[size][size]; int main(int argc, char** argv) { // assign numbers matrix , b (i = 0; < size; i++) { (j = 0; j < size; j++) { g_a[i][j]=(double)rand()/rand_max; g_b[i][j]=(double)rand()/rand_max; } } multiplymatricessequential(g_a,g_b,g_ans); return 0; } //..........
note added "g_" prefix "matrices" names.
also know stack or "local storage" used storing temporary variables , function arguments allocated when function in belong called , "removed" when returns. stack size fixed , if there no space in create them program crashes. global variables on other hand doesn't have memory limit space need automatically allocated on compile-time. life-time equals application run-time "matrices" because had created them "main" function. choice simple - want waste program time allocating & "removing" data , risking stack overflow or big data without hassle - using global variables. or if using real data - becoming friendlier dynamic memory allocation.
Comments
Post a Comment