-1

While compiling a problem:
Geeks For Geeks: First Repeating Element on a Windows operating system I noticed that I was not getting any output for my solution. But when I compiled the same code on a Linux operating system and on online compilers, it worked absolutely fine without producing any errors.

Code:

#include <bits/stdc++.h>
using namespace std;

int main() {

    int n;
    cin>>n;
    int arr[n];

    for(int i=0; i <n; i++) {
        cin>>arr[i];
    }

    int size= 1e6+1; // WA on windows
    int A[size];
    int min_index=INT_MAX;

    for(int i=0; i<size; i++) {
        A[i]=-1;
    }

    for(int i=0; i<n; i++) {
        if(A[arr[i]]!=-1)
            min_index=min(min_index, A[arr[i]]);
        else
            A[arr[i]]=i;
    }

    if(min_index==INT_MAX) 
        cout<<"-1";
    else
        cout<< min_index+1;

    return 0;
}

Sample Test Case:

7
1 5 3 4 3 5 6

Expected output:

2

Output on Windows: Screenshot

Output on Linux: Screenshot

Explanation for the program from line 14 of code:

I created an array A of size 1e6+1 to store value i on its arr[i]th index.
Array A was earlier initialized with value -1. It runs for n number of times and variable min_index stores the index of the least repeating number from the array arr.
After initializing smaller values of array int size = 10 and using very small test cases(also max value of arr[i] is lesser than size of A; I realize that the program runs perfectly in Windows.

As far as I understand, Windows might be having some trouble intializing arrays of such large length (Please correct me if I'm wrong). But why isn't it the same in the case of Linux?

Dipanshu
  • 23
  • 5
  • 7
    To begin with: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) To continue: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) And while `using namespace std;` is [a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) it's often considered okay for small and simple examples. – Some programmer dude Jan 20 '22 at 12:40
  • 7
    And to finish: Don't use so-called "competition" or "online judge" sites as any kind of teaching or learning resources. They are *not* that, and could be actively harmful for your proper learning. Get [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take classes to learn C++ (and programming in general) properly. – Some programmer dude Jan 20 '22 at 12:42
  • 1
    And I'm actually surprised that the program even run on Windows, considering that your VLA (Variable-Length Array) `A` is four million bytes large. The default stack-size (and local variables are usually stored on the stack) on Windows is only a single MiB. Also `1e+1` is a *floating point* value (of type `double`) and not more readable than `1000001`. – Some programmer dude Jan 20 '22 at 12:47
  • Or perhaps the problem is that the VLA `A` really is too large and your program *crashes*? That means you need to learn something else not taught by such sites: ***Debugging***. – Some programmer dude Jan 20 '22 at 12:50
  • `int A[size];` No. The following is proper C++ code: `std::vector A(size);`. Second, as already mentioned, use a C++ book to learn C++. If you had done that, you would not have made the mistake, since no good C++ book shows declaring arrays using a runtime variable. – PaulMcKenzie Jan 20 '22 at 13:44
  • The problem could be: `int A[1e6+1]` that is a large array. Checkout this: [Is there a max array length limit in C++?](https://stackoverflow.com/a/216731/14065). A simple solution may be to change that to a `std::vector`. – Martin York Jan 24 '22 at 21:45

1 Answers1

3

#include <bits/stdc++.h> is not standard C++, hence you should not expect it to be portable.

int arr[n]; is not standard C++. Some compilers offer variable length arrays as extension, but it isnt portable. Same goes for int A[size];.

Sadly most of the C++ code presented on that site is not proper C++ code but some dialect.

For more details see: Why should I not #include <bits/stdc++.h>? and Why aren't variable-length arrays part of the C++ standard?. That Q&As should also explain the standard portable alternatives.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185