-3

I don't have a clue why this program is crashing? Here is my code that I'm working on. Thanks.

Also someone said that int a[n]; is VLA, which is not legal in C++... But I have used the same syntax for my other programs and it has worked.

#include<iostream>
#include<climits>

using namespace std;

int main()
{
    int n;
    cin>>n;

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

    const int N = 1e6 + 2;
    int idx[N];
    for(int i = 0; i<N; i++)
    {
        idx[i] = -1;
    }
    int minidx = INT_MAX;
 
    for(int i = 0; i<n; i++)
    {
        if (idx[a[i]] != -1)
        {
            minidx = min(minidx, idx[a[i]]);
        }
        else
        {
            idx[a[i]] = i;
        }
        
    }
    if (minidx == INT_MAX)
    {
        cout<<"-1"<<endl;
    }
    else
    {
        cout<<minidx + 1<<endl;
    }
    
    return 0;
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 1
    Your `idx` array is *very* big. If that's allocated on the stack, it could be an issue. Using `std:vector` (for that and for your VLA) is a better option. – Adrian Mole Jun 28 '21 at 09:10
  • 2
    usually I mention this just because, but here it is actually relevant: [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) – 463035818_is_not_an_ai Jun 28 '21 at 09:12
  • 1
    it "worked" because compilers (eg gcc) offer them as a non-standard extension. I would prefer `std::vector` to a compiler extension when possible – 463035818_is_not_an_ai Jun 28 '21 at 09:13
  • 1. What this code suppose to do? 2. What is the input which lead to a crash. 3. Here is my attempt to reproduce your issue, without trying understund your code: https://godbolt.org/z/3PWfhfPPj – Marek R Jun 28 '21 at 09:22
  • Gotcha everybody! – CodeCub Jun 28 '21 at 09:36
  • _'my program is crashing'_ is quite general description. Can you specify where it crashes? E.g. if you run the program in a debugger, it will tell you where - at which instruction - the crash occurs. You may also try to narrow down the problem by separating parts of your program with some diagnostic printouts – then see how far it managed to get... – CiaPan Jun 28 '21 at 10:05

2 Answers2

2

Try to dynamic allocate the memory. You have 1M of elements in that array, which might be a little to much for the stack.

Neko
  • 21
  • 2
0

As @Neko said, your stack can not hold the array.

In addition, I like to say that since you tagged this question 'C++' perhaps you go with std container such as std::vector

#include<iostream>
#include<climits>

int main()
{
    int n;
    std::cin>>n;

    std::vector<int> a(n);
    for(int i= 0; i<n; i++)
    {
        std::cin>>a[i];
    }

    const int N = 1e6 + 2;
    
    std::vector<int> idx(M);
    std::fill(idx.begin(), idx.end(), -1);
    
    int minidx = INT_MAX;
 
    for(int i = 0; i<n; i++)
    {
        if (idx[a[i]] != -1)
        {
            minidx = min(minidx, idx[a[i]]);
        }
        else
        {
            idx[a[i]] = i;
        }
        
    }
    if (minidx == INT_MAX)
    {
        std::cout<<"-1"<<endl;
    }
    else
    {
        std::cout<<minidx + 1<<endl;
    }
    
    return 0;
}
schorsch312
  • 5,553
  • 5
  • 28
  • 57