1

I have extracted the code which get problem The following code define an 2D array with vector. and then print the 2D array. When I use debug mode to run it, it print the right answer, But when I run it without debug(ie. the release mode). sometimes it print the whole 2D array, sometimes it print part of it, and then just exit without any message. why this happened? Thank you!.

running environment:
Windows 10 version: 10.0.16299.125
Clion 2017.3
Mingw64 5.0

Clion's CmakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(Test)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(Test ${SOURCE_FILES})


#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<vector<int>> graph{{0, 4,  0, 0,  0,  0,  0, 8,  0},
                              {4, 0,  8, 0,  0,  0,  0, 11, 0},
                              {0, 8,  0, 7,  0,  4,  0, 0,  2},
                              {0, 0,  7, 0,  9,  14, 0, 0,  0},
                              {0, 0,  0, 9,  0,  10, 0, 0,  0},
                              {0, 0,  4, 14, 10, 0,  2, 0,  0},
                              {0, 0,  0, 0,  0,  2,  0, 1,  6},
                              {8, 11, 0, 0,  0,  0,  1, 0,  7},
                              {0, 0,  2, 0,  0,  0,  6, 7,  0}};


    for(int i = 0; i < graph.size(); i++)
    {
        for(int j = 0; j < graph[i].size(); j++)
        {
            cout  << i << "-" << j  << ": "<< graph[i][j] << endl;
        }
        cout << endl;
    }
    return 0;
}

the output is not stable, sometime succeed, sometime failed, following is two of the wrong outputs The first wrong output:

E:\coding\LCCPP\Test\cmake-build-debug\Test.exe
0-0: 0
0-1: 4
0-2: 0
0-3: 0
0-4: 0
0-5: 0
0-6: 0
0-7: 8
0-8: 0

1-0: 4
1-1

1-1:
Process finished with exit code 0

The second wrong output:

E:\coding\LCCPP\Test\cmake-build-debug\Test.exe
0-0: 0
0-1: 4
0-2: 0
0-3: 0
0-4: 0
0-5: 0
0-6: 0
0-7: 8
0-8: 0

1-0: 4
1-1: 0
1-2: 8
1-3: 0
1-4: 0
1-5: 0
1-6: 0
1-7: 11
1-8: 0

2-0: 0
2-1: 8
2-2: 0
2-3: 7
2-4: 0
2-5: 4
2-6: 0
2-7: 0
2-8: 2

3-0: 0
3-1: 0
3-2: 7
3-3: 0
3-4: 9
3-5: 14
3-6: 0
3-7: 0
3-8: 0

4-0: 0
4-1: 0
4-2: 0
4-3: 9
4-4: 0
4-5: 10
4-6: 0
4-7: 0
4-8: 0

5-0: 0
5-1: 0
5-2: 4
5-3: 14
5-4: 10
5-5: 0
5-6: 2
5-7: 0
5-8: 0

6-0: 0
6-1: 0
6-2: 0
6-3: 0
6-4: 0
6-5: 2
6-6: 0
6-7: 1
6-8: 6

7-0: 8
7-1: 11
7-2: 0
7-3: 0
7-4: 0
7-5: 0
7-6: 1
7-7: 0
7-8: 7

8-0: 0
8-1: 0
8-2: 2
8-3: 0
8-4: 0
8-5: 0
8
Process finished with exit code 0

The right output:

E:\coding\LCCPP\Test\cmake-build-debug\Test.exe
0-0: 0
0-1: 4
0-2: 0
0-3: 0
0-4: 0
0-5: 0
0-6: 0
0-7: 8
0-8: 0

1-0: 4
1-1: 0
1-2: 8
1-3: 0
1-4: 0
1-5: 0
1-6: 0
1-7: 11
1-8: 0

2-0: 0
2-1: 8
2-2: 0
2-3: 7
2-4: 0
2-5: 4
2-6: 0
2-7: 0
2-8: 2

3-0: 0
3-1: 0
3-2: 7
3-3: 0
3-4: 9
3-5: 14
3-6: 0
3-7: 0
3-8: 0

4-0: 0
4-1: 0
4-2: 0
4-3: 9
4-4: 0
4-5: 10
4-6: 0
4-7: 0
4-8: 0

5-0: 0
5-1: 0
5-2: 4
5-3: 14
5-4: 10
5-5: 0
5-6: 2
5-7: 0
5-8: 0

6-0: 0
6-1: 0
6-2: 0
6-3: 0
6-4: 0
6-5: 2
6-6: 0
6-7: 1
6-8: 6

7-0: 8
7-1: 11
7-2: 0
7-3: 0
7-4: 0
7-5: 0
7-6: 1
7-7: 0
7-8: 7

8-0: 0
8-1: 0
8-2: 2
8-3: 0
8-4: 0
8-5: 0
8-6: 6
8-7: 7
8-8: 0


Process finished with exit code 0
3
  • 3
    I think there is no problem with this code, although it's better to use size_t instead of int. Show all code / another part of code. Jan 18, 2018 at 4:40
  • 1
    @АндрейМоскаленко Better to use auto :)
    – P. Dmitry
    Jan 18, 2018 at 4:51
  • I am not able to find an error in your code. Nevertheless, I compiled it on RetHat 6 with gcc, clang and the intel compiler. I changed the optimisation level. In all cases debug and release have the very same output. Jan 18, 2018 at 7:40

2 Answers 2

0

There is no actual problems with code, it is likely issue of IDE not providing output. Essentially your program exits before whole pipe buffer was printed in console. Pipe is closed without any error when child process exits, and parent process have nothing to print. What happens if you pause program at end?

While it is not good to not provide "return" in non-void function, mingw adds equivalent of return 0; to main()...

PS: Consider usage of range-fors:

int main()   
{
    using namespace std;
    vector<vector<int>> graph{{0, 4,  0, 0,  0,  0,  0, 8,  0},
                              {4, 0,  8, 0,  0,  0,  0, 11, 0},
                              {0, 8,  0, 7,  0,  4,  0, 0,  2},
                              {0, 0,  7, 0,  9,  14, 0, 0,  0},
                              {0, 0,  0, 9,  0,  10, 0, 0,  0},
                              {0, 0,  4, 14, 10, 0,  2, 0,  0},
                              {0, 0,  0, 0,  0,  2,  0, 1,  6},
                              {8, 11, 0, 0,  0,  0,  1, 0,  7},
                              {0, 0,  2, 0,  0,  0,  6, 7,  0}};


    for(auto &line: graph)
    {
        int i = 0, j= 0;
        for(auto v : line )
        {
            cout  << i << "-" << j  << ": "<< v << endl;
            j++;
        }
        i++;
        cout << endl;
    }
    return 0;
}

( http://en.cppreference.com/w/cpp/language/range-for )

2
  • Thank you, I sleep the program at the end of main function (before return) for 3 seconds, (sometimes) the program still print part of the answer, then after waiting for 3 seconds, the program finished. range for is not suitable for my real program(I have simplified the code with problem), but your thought on IDE give me a good prompt, Maybe I should reinstall Clion and mingw.
    – Damon Hu
    Jan 18, 2018 at 7:36
  • @Damon Hu it is well known issue with various IDE and it is defined by architecture of OS. Sounds like it doesn't fit the buffer. Is output very large& Default size of buffer is 32k on most OSes.. Jan 19, 2018 at 0:25
-1

Maybe check this solution by adding std::flush:

for(auto v : line )
{
    cout  << i << "-" << j  << ": "<< v << endl << flush;
    j++;
}

It will 'force' all outputs. BTW in my opinion it is some problem with code optimization.

1
  • std::endl flushes anyway. So an extra flush doesn't do anything. Oct 20, 2022 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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