I was reading Use Python for Scientific Computing, and decided to test the code myself. So the C++ code is (with a bit modification)
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cstring>
int main() {
std::clock_t begin = std::clock();
double a1[500][500];
double a2[500][500];
double a3[500][500];
memset(a1, 0, 500*500*sizeof(double));
memset(a2, 0, 500*500*sizeof(double));
memset(a3, 0, 500*500*sizeof(double));
int i, j, k;
for(i = 0; i < 500; i++) {
for(j = 0; j < 500; j++) {
for(k = 0; k < 500; k++) {
a3[i][j] += a1[i][k] * a2[k][j];
}
}
}
std::clock_t end = std::clock();
std::cout << (double)(end - begin) / (double)CLOCKS_PER_SEC<<std::endl;
return 0;
}
It is a very simple code, but weirdly no output at all is generated. Not 0, but simply nothing. I tried VC11 and MinGW 4.7, but they both produce nothing. Only when the for
loop inside is removed will this code produce an output, which is 0.
And if I debug in VS 2012, an exception of "stack overflow" will be thrown, while no error happens if not in debug mode.
What is the reason for this weird behavior?
edit
So I used new
and this time there is a normal output 0.83.
Still, I find it curious that a stack overflow error is not shown, but the program simply exits without giving an output.
0.83
[varies] ongcc 4.6.3