1

Whenever I am writing this following code, I am getting garbage(unexpected) output in some online compiler, but if I use code block then getting satisfied output. So my question is why I am getting this type of output?

for example, if I input

5 7
+ 5
- 10
- 20
+ 40
- 20

then I am getting

22 1

in the code block. But in the online compiler, it's something else.

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int have, n, i;
    int kid=0;

    cin>>n>>have;

    int line[n];

    for(i=0;i<n;i++)
    {
        cin>>line[i];

        if(line[i]>=0)
            have+=line[i];
        else
        {
            if(have>=abs(line[i]))
                have+=line[i];
            else
                kid++;
        }
    }

    cout<<have<<" "<<kid<<endl;

}
mahin
  • 57
  • 6

1 Answers1

1

The main problem I can see in your code is this:

int line[n];

This is known as a VLA (Variable Length Array) and it is not supported in C++. It is valid in C. Most compilers still allow this behaviour due to the fact that C++ is based on C, but it is not valid C++ code. In a previous question, I found out that clang supports designated initializers, when gcc and vc++ did not. The reason is because some compilers like clang, support c99-extensions by default. My point is that just because the code compiles, it doesn't mean it's always right.

If you compile with the -pedantic argument, you will see that the compiler is warning you about this being a C99 feature. Have a look at the rextester example here. From the comments below, using -pedantic-errors in the compiler flags, will prompt an error.

If you know the size of the array before run-time, then you should use a static array int line[4];, but if you don't then you need to use a dynamic array. std::vector is essentially a dynamic array that also handles memory for you. It's easy to use and very efficient. std::vector<int> line;

You can read more about the vector container here: http://www.cplusplus.com/reference/vector/vector/

Btw, I tried your code in rextester, ideone and repl.it and I got the same results: 22 1. I think what you are witnessing it undefined behaviour.

Also, you can qualify int n with constexpr and it'll be fine.

constexr int n = 200;
int line[n]; //now it's ok.

But this again means that you know the size of the array at compile time.

Constantinos Glynos
  • 2,952
  • 2
  • 14
  • 32
  • `int[n];` for non compile time constant `n` is either forbidden or allowed as compiler extension, but it does not invoke undefined behviour – 463035818_is_not_an_ai Feb 13 '19 at 18:04
  • @user463035818: The C++ standard does not accept VLAs which makes it forbidden, but compilers support them and not always with the need for an extension. So what happens when `int [n]` is both forbidden and supported? I guess in that case, C++ would say: "I do not accept this code, but if you still want to go for it, then you're on your own!". That's what I meant by undefined behavior. And again, it's only a guess... – Constantinos Glynos Feb 13 '19 at 23:20
  • when the compiler defines what happens for VLAs then it is not undefined. Its just not standard c++ – 463035818_is_not_an_ai Feb 14 '19 at 09:18
  • @user463035818: I see your point. One can argue however that ok, it is defined by the compiler (if you're lucky because not all compilers support them - and what if it's a custom compiler), but it is not defined in the standard. So, defined by the compiler, undefined by the standard. Overthinking it.... – Constantinos Glynos Feb 14 '19 at 09:49
  • well I have to admit, i was not completely right, `int[n];` only produces an error with `-pedantic-errors` but not with `-pedantic` (only a warning), while `-pedantic` should be enough to make the compiler behave standard compliant, I am still a bit confused why `int[n]` is not required to result in an error, will have to do some more reading... – 463035818_is_not_an_ai Feb 14 '19 at 10:51
  • @user463035818: Good point about the `-pedantic-errors`! I'll need to put that in the answer too. I think it may have something to do with it being valid in C and most compilers tend to allow C code. Not sure though. Please post a comment if you find something on this. – Constantinos Glynos Feb 14 '19 at 11:35
  • @user463035818 `-pedantic` *does* make it standard compliant. The standard requires a diagnostic message if the code is ill-formed, but it doesn't have to be a error; a simple warning is enough. – HolyBlackCat Feb 14 '19 at 11:40