2

I have a simple C++ code, it is giving garbage values when executed in my friend's Windows 10 machine with Mingw g++. But it gives a correct output when executed in my Ubuntu(g++) and various other online compilers.

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.

This is the code that I am executing - 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";
        }
    }
}

I have attached the outputs from ubuntu g++(left) , Mingw g++ (black screen - right).
Is something wrong with the code I wrote or is there some issue with mingw?

EDIT:
The same code works in mingw g++ when I use long long for dist instead of long.

enter image description here

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Parth Kapadia
  • 507
  • 6
  • 18
  • 2
    You wrote non-standard C++. Non-standard results shouldn't be surprising. – sweenish Mar 17 '21 at 17:02
  • @drescherjm, In that case, it shouldn't work even when dist is initialized with long long instead of long. But when I do that, the code works fine (so I think it is something related to overflow. And if it is so, why is it working on some compilers and not in MinGW g++- correct me if I am wrong). – Parth Kapadia Mar 17 '21 at 17:11
  • 1
    `if (dist[i][j] > dist[i][k] + dist[k][j]) {` INT_MAX + INT_MAX overflows long on some platforms so if both `dist[i][k]` and `dist[k][j]` are INT_MAX you have UB. Actually INT_MAX + any positive integer will overflow on platforms where long and int are the same size – drescherjm Mar 17 '21 at 17:13

1 Answers1

3

As @drescherjm pointed out in the comments,if (dist[i][j] > dist[i][k] + dist[k][j]) was causing overflow (when used with long).

This was working for other compilers and not on the windows machine with MinGW g++ because it allocated 4 bytes for long. It worked with long long as it allocated 8 bytes for long long and hence overflow didn't occur.

When I ran the following code -

    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++ gave the output as :

4
4
8

while Ubuntu g++ gave the output as:

4
8
8

The C++ standard says:

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.

You can read about this more over here - What does the C++ standard state the size of int, long type to be?

Parth Kapadia
  • 507
  • 6
  • 18