0

Can someone please explain why my code is causing a stack overflow error while a similar code is not?

My Code

#include<iostream>

int main(){

using namespace std;

   int n, k;
   int score[n];
   int pass=0;
   cin >> n >> k;
   
   for(int i=0; i<n; i++){
       cin >> score[i];
   }
    for(int i =0; i<n; i++){
        if(score[i]>=score[k-1] && score[i]>0){
            pass++;
        }
    }  
    cout << pass;
    return 0;
}

Similar Code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    int count=0;
    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }

    for(int i=0;i<n;i++)
    {
        if(a[i]>=a[k-1] && a[i]>0)
        {
            count++;
        }
    }
    cout<<count;
}

I tried changing the index range and also the variable names.

  • `int n, k; int score[n];` -- This is not valid C++, regardless of whether `n` has a value or not. Arrays in C++ must have their size denoted by a compile-time constant, not a runtime variable. Dynamic arrays in C++ are done by using `std::vector`. – PaulMcKenzie May 28 '23 at 18:45
  • [See this](https://godbolt.org/z/vMxMKqdGY). Also, it looks like you're learning C++ from a bad C++ coding website, and not good C++ books. A good C++ book would never show declaring arrays this way. – PaulMcKenzie May 28 '23 at 18:51
  • `int n, k; int score[n]; cin >> n ...` - That's not valid C++. Standard C++ does not support VLAs (Variable Length Arrays) - you want [std::vector](https://en.cppreference.com/w/cpp/container/vector) when you want a dynamically sized array. – Jesper Juhl May 28 '23 at 23:49
  • Your "Similar code" is *horrible* - See [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) & [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and it also uses a VLA which is not part of standard C++ - see [Variable Length Array (VLA) in C++ compilers](https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers) and https://eel.is/c++draft/dcl.array#:declarator,array – Jesper Juhl May 28 '23 at 23:55

1 Answers1

1

In the first example, the score array is initialized with a length of n, but n was never assigned a value and likely is zero.

In the second example, you wait to initialize the a array until after n was given a value. This is what you want to do.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • I just wanted to ask why we cant initialize an array with a length variable first and then assign the value. – Ajinkya Gawali May 28 '23 at 18:07
  • 1
    @AjinkyaGawali Arrays must have a fixed length, so the length has to be known when you create the array. If you assign it to a variable that can change, then the length of the array wouldn't be fixed. There are – Michael M. May 28 '23 at 18:09
  • 1
    @AjinkyaGawali The **order** of your statements matters. If you want a statement to use the value of a variable then you must give that variable a value **before** you use the variable. This will seem completely obvious once you learn a bit more programming, but you aren't the first beginner to ask about this. – john May 28 '23 at 18:43
  • 3
    @MichaelM. The code is not valid C++, regardless of whether `n` has a vaule or not. You should have mentioned that up front. – PaulMcKenzie May 28 '23 at 18:46
  • [See this](https://godbolt.org/z/vMxMKqdGY). – PaulMcKenzie May 28 '23 at 18:52
  • "but n was never assigned a value and likely is zero" - No, it is not "*likely zero*", it has an indeterminate value, and reading it (as in the array declaration) is [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub). – Jesper Juhl May 28 '23 at 23:56