Mingw g++ 显示垃圾输出,而它在其他 IDE 和 Ubuntu g++ 中运行

Mingw g++ shows garbage output, while it runs in other IDEs and Ubuntu g++

我有一个简单的 C++ 代码,它在我朋友的 Windows 10 机器上使用 Mingw g++ 执行时给出垃圾值。但是在我的 Ubuntu(g++) 和各种其他在线编译器中执行时它会给出正确的输出。

g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

这是我正在执行的代码 - https://pastebin.com/zn0aEQe1

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int v;
    cout << "\nEnter the number of vertices: ";
    cin >> v;
    int adj[v][v];
    long dist[v][v];
    int next[v][v];

    for (auto i = 0; i < v; i++)
        for (auto j = 0; j < v; j++)
            cin >> adj[i][j];

    for (auto i = 0; i < v; i++) {
        for (auto j = 0; j < v; j++) {
            if (i == j) {
                dist[i][j] = 0;
                next[i][j] = i;
            }
            else if (adj[i][j]) {
                dist[i][j] = adj[i][j];
                next[i][j] = j;
            }
            else {
                dist[i][j] = INT_MAX;
                next[i][j] = -1;
            }
        }
    }

    for (auto k = 0; k < v; k++)
        for (auto i = 0; i < v; i++)
            for (auto j = 0; j < v; j++) {
                if (dist[i][j] > dist[i][k] + dist[k][j]) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                    next[i][j] = next[i][k];
                }
            }
    cout << "\nThe Shortest Distances are: \n";

    for (auto i = 0; i < v; i++) {
        for (auto j = 0; j < v; j++) {
            cout << dist[i][j] << " ";
        }
        cout << "\n";
    }

    cout << "\nThe Shortest Paths are: \n";
    // for printing path
    for (int i = 0; i < v; i++) {
        for (int j = i + 1; j < v; j++) {
            int u = i, v = j;
            cout << u;
            while (u != v) {
                u = next[u][v];
                cout << "->" << u;
            }
            cout << "\n";
        }
    }
}

我附上了 ubuntu g++(左)、Mingw g++(黑屏 - 右)的输出。
是我写的代码有问题还是mingw有问题?

编辑:
当我将 long long 用于 dist 而不是 long.

时,相同的代码在 mingw g++ 中工作

正如@drescherjm 在评论中指出的那样,if (dist[i][j] > dist[i][k] + dist[k][j]) 导致溢出(与 long 一起使用时)。

这适用于其他编译器,而不适用于使用 MinGW g++ 的 windows 机器,因为它为 long 分配了 4 个字节。它与 long long 一起工作,因为它为 long long 分配了 8 个字节,因此没有发生溢出。

当我运行下面的代码-

    int a;
    long b;
    long long c;
    a = INT_MAX;
    b = INT_MAX;
    c = INT_MAX;

    cout<<sizeof(a)<<endl<<sizeof(b)<<endl<<sizeof(c);

MinGW g++ 给出的输出为:

4
4
8

而 Ubuntu g++ 给出的输出为:

4
8
8

C++ standard 说:

There are five signed integer types : "signed char", "short int", "int", "long int", and "long long int". In this list, each type provides at least as much storage as those preceding it in the list.

您可以在此处阅读更多相关信息 - What does the C++ standard state the size of int, long type to be?