0

first timer here!

This might seem like a really dumb question but I have tried a lot of different threads on this website already but without any luck... hope that someone might have an idea, thanks in advance!

I get an error on input n for the array below (image attached too):

int n, q;
cin >> n >> q;

long long BITree[n+1];

return 0;

Error shows: "expression must have a constant value C/C++(28)"

This error message has several posts on this website already however I am positive that this should work as I do not get this error when trying the same code on my laptop (also able to compile and run on the laptop).

Things that I've tried so far:

  • Checked all extension settings (as well as versions),
  • Checked all Visual Studio Code Settings (as well as version),
  • Checked that I am using the same compiler version
    • Currently using: g++ (Rev5, Built by MSYS2 project) 11.2.0

This has bugged me for quite some time and I have taken the long road of always using the other threads to solve it but I really want to find out why this is happening. Does anyone have any ideas or could link me to a more appropriate thread about fixing this?

Thanks again!

Capplo
  • 1
  • 2
  • 3
    [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). It may work on your laptop because some compilers have extensions enabled by default. You need to tell them to compile standard C++ and report errors / warnings for non-standard C++ – 463035818_is_not_an_ai Apr 05 '22 at 15:40
  • 2
    `long long BITree[n+1];` --> `std::vector BITree(n+1);` -- Then you will have no issues with any C++ compliant compiler. – PaulMcKenzie Apr 05 '22 at 15:41
  • It sounds like you already know why it doesn't work and what you can do instead. Would it be fair to reframe this question as "Help me figure out what's different between my laptop and my desktop, because I _think_ all relevant compilers and IDE settings are the same"? – Nathan Pierson Apr 05 '22 at 15:42
  • *Checked all Visual Studio Code Settings (as well as version),* -- Visual Studio Code is not a C++ compiler. Changing IDE settings, unless you're changing the underlying C++ compiler options, won't do anything. – PaulMcKenzie Apr 05 '22 at 15:44
  • _"I am positive that this should work"_ I think the simplest answer may be that "It runs on my machine" does not imply "It is correct". C++ compilers are required to report an error for certain mistakes, but not every mistake. – Drew Dormann Apr 05 '22 at 15:44
  • @463035818_is_not_a_number Thank you for the link, I'll check that out! – Capplo Apr 05 '22 at 15:48
  • @PaulMcKenzie This did work but is there a reason to why I *have* to use a vector to solve this? – Capplo Apr 05 '22 at 15:48
  • @NathanPierson That seems very reasonable ,ty for the heads up! – Capplo Apr 05 '22 at 15:48
  • @DrewDormann I understand the perspective but it feels off as I have used this way to solve issues in Kattis (Different problems that have been used around the world in competitions and are run on a centralized machine). I might still be completely off however. Thank you for the input – Capplo Apr 05 '22 at 15:51
  • 1
    @CltCGaming *This did work but is there a reason to why I have to use a vector to solve this?* -- Dynamic arrays in C++ are easily achieved by using `std::vector`. What you had is a non-starter -- variable length arrays do not exist in standard C++. The `std::vector` has been around C++ for 24 years now. It isn't some "third-party" add-on that was recently developed. So not using it for the purpose it is intended for doesn't make a lot of sense. – PaulMcKenzie Apr 05 '22 at 15:54
  • @CltCGaming the short answer is that it's invalid C++, however certain compilers allow it as a compiler extension to the language, because it is valid syntax in certain versions of the C language. – Drew Dormann Apr 05 '22 at 15:54
  • 1
    Standard library containers are vastly superior to native arrays for most purposes for a number of reasons, including (but not limited to) automatic memory management, size determination, and not decaying to a pointer at the least provocation. – Fred Larson Apr 05 '22 at 15:56
  • @PaulMcKenzie Thank you for the explanation! But just to clear up the array part, if I every would want to use array instead of vector I should use the "correct" way (as the other threads on the website are doing) by using pointers and declaring new int[n] instead? Sorry if the question was a bit hard to understand. – Capplo Apr 05 '22 at 16:03
  • @DrewDormann Thanks you, I'll try to pound that into my head from here on! – Capplo Apr 05 '22 at 16:04
  • 1
    @CltCGaming Using `new[]` and `delete[]` is now discouraged with modern C++ user-based applications. The `std::vector` does exactly all of that for you, but wrapped inside a class, with no bugs, and no memory leaks. In many code reviews, using `new[]` and `delete[]` needs to be justified as to why it's being done (instead of simply using `std::vector`). – PaulMcKenzie Apr 05 '22 at 16:05
  • Some handy reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Apr 05 '22 at 16:25

0 Answers0