1

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.

9
  • 3
    How much time did you wait?
    – UmNyobe
    Commented Apr 18, 2013 at 7:59
  • 1
    I got 0.83[varies] on gcc 4.6.3 Commented Apr 18, 2013 at 8:01
  • I am wondering if this did not give you stack overflow. Try using new instead of local array declaration.
    – sarat
    Commented Apr 18, 2013 at 8:04
  • 1
    Why do people vote to close this? It's a perfectly valid question.
    – jogojapan
    Commented Apr 18, 2013 at 8:12
  • 1
    Are you saying the program doesn't even crash (other than the VS2012 in debug mode)? It just terminates without outputting anything - not even a dialog box or message that says it "stopped working" or something? Commented Apr 18, 2013 at 8:27

5 Answers 5

1

You ran in two things:

  1. Compiler\Environment paramater difference. The default stack size (a1, a2 and a3 are allocated in the stack) varies between compilers, operating systems. So in the ones where the stack is not enough to fit those variables you get a stack overflow exception
  2. Difference in optimization level (SuvP got 0.83 seconds): This code execution time will be different depending on the optimization. The compiler can notice that the loop does nothing in particular and just remove it. It can go even further and realize that the memset are not useful and remove them too. But if the loop is executed as it is, then there are 125 millions operation on double, which will take much more than mere 0.8 seconds.

The test in the post you linked is flawed from the beginning...

7
  • How does this explain not getting any output?
    – jogojapan
    Commented Apr 18, 2013 at 8:19
  • "there are 125 millions operation on double, which will take much more than mere 0.8 seconds" - 0.8 seems to be realistic to me. Commented Apr 18, 2013 at 8:20
  • @jogojapan I cannot do the test now, but if we initialize the arrays with random values, forcing the loop to execute, it will take several seconds to complete.
    – UmNyobe
    Commented Apr 18, 2013 at 8:21
  • @UmNyobe Yes, sure. But if the loop is optimized away, the program will output 0 and return immediately. If, on the other hand, there is a stack overflow, you should get a crash or abort of some kind. I don't understand how this explains that nothing happens. (Assuming that "nothing happens" means that the program returns without outputting anything. Perhaps that's not what the OP actually means.)
    – jogojapan
    Commented Apr 18, 2013 at 8:22
  • @jogojapan, on Windows 7 and maybe on Vista you should get the application have stopped working window...
    – joy
    Commented Apr 18, 2013 at 8:26
1

It's definitely a stack overflow. On VS 2008 it gives me Unhandled exception at 0x00ef18d7 in Test.exe: 0xC00000FD: Stack overflow.

And error occurs in chkstk.asm at test command:

; Find next lower page and probe
cs20:
        sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
  -->   test    dword ptr [eax],eax     ; probe page.
        jmp     short cs10

_chkstk endp

To fix this use an allocation via new allocator as @sarat mentioned:

double** a1 = new double*[500];
for(int i = 0; i < 500; ++i)
    a1[i] = new double[500];
1

Visual Studio:

You are trying to put(commit) 6MB on stack and on x86 and x64 the default reserved stack size is 1MB, on Itanium the default reserved stack size is 4MB. You can't commit more than you have reserved.

In Visual Studio to change the predefined reserved stack size, you should go to Project Properties->Linker->System->Stack Reserve Size, set the value to 10000000(10MB), and you will not have any problems.

0

If not in debug mode your code simply crash/freezes, fix that stack overflow and it will complete.

Increase stack size (linker stack option).

0

This will cause stack overflow. Try using new instead of local array declaration.

This should help: How do I use arrays in C++

Not the answer you're looking for? Browse other questions tagged or ask your own question.