2

I was wondering what are some alternatives to doing the following code snippet in c++.

int i;
cin >> i;
int arr[i];

I have recently started looking into competitive programming and trying to learn more.

EDIT: For those comments about this not being cpp. It compiles successfully with the makefile I am using in my class which is using gcc -std=c++11 -o a.exe main.cpp and returns to the console when I input a length of 1

array length: 1
Amit G.
  • 2,546
  • 2
  • 22
  • 30
adarian
  • 334
  • 7
  • 24
  • 2
    You can also use something like this: `int number = 0; while (cin >> number) { myVector.push_back(number); }`, but, here, you are specifying the size directly. – NutCracker Apr 07 '19 at 04:17
  • 9
    This code snippet is not valid C++; many compilers allow variable-length arrays as an extension, but in standard C++ you need to know the size at compile time unless you want to mess with dynamic arrays using `new[]`. – Daniel H Apr 07 '19 at 04:17
  • *What are different ways...* -- The way you're showing us in the question isn't C++, so you can forget about the way you're showing us in your question. – PaulMcKenzie Apr 07 '19 at 04:39
  • Learn C++, then start competing.You are at a serious competitive disadvantage if you try to do it the other way around. [Here is a list of good references](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Apr 07 '19 at 04:59
  • In CP I usually allocate all my arrays with fixed sizes set at the maximum specified in the problem. – eesiraed Apr 07 '19 at 05:22

3 Answers3

11

You should use std::vector instead in C++, e.g.

int i;
cin >> i;
std::vector<int> arr(i);

BTW: VLA is not supported by C++ standard. Also see Variable Length Array (VLA) in C++ compilers

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 3
    This should be the accepted answer. Don't know why the OP hasn't done so. – PaulMcKenzie Apr 07 '19 at 04:38
  • It seems like the code snippet above works because of my gcc version and not specifically the c++ [explanation](https://stackoverflow.com/questions/8593643/does-c-support-variable-length-arrays) – adarian Apr 07 '19 at 05:37
  • 2
    @PaulMcKenzie - the OP probably won't accept the answer because of a mistaken belief that "gcc accepts his code with VLAs so the code is correct". – Peter Apr 07 '19 at 06:07
  • 1
    This is why I wished gcc (and other) compilers would turn the VLA option *off* by default. Many new programmers have the mistaken belief that their "VLA" code is correct when it really isn't. – PaulMcKenzie Apr 07 '19 at 22:44
3

One way is:

int i = 0;

cin >> i;

auto arr = std::make_unique<int[]>(i); // #include <memory>
        // Or: std::make_shared<int[]>(i);

Another way is to use std::vector:

int i = 0;

cin >> i;

std::vector<int> arr(i); // #include <vector>
Amit G.
  • 2,546
  • 2
  • 22
  • 30
1

Your code is not C++. It uses a C language feature (from the C99 version of the C standard) called "variable-length arrays" - where arrays on the stack can have a length determined at run-time.

Variable-length arrays are considered are dangerous and considered a rather bad idea; see:

Why aren't variable-length arrays part of the C++ standard?

If you compile your code while telling the compiler to only accept standard-compliant code, it will fail: gcc -std=c++11 -pedantic-error. Try it on GodBolt.

In C++ the size of an array is set at compile-time, period. Like other users suggested, you can use dynamic allocation to obtain a run-time-determined-length contiguous area in memory, of your choice of size: Using std::vector, std::unique_ptr, std::shared_ptr or even plain allocation with new (although the latter is not recommended).

einpoklum
  • 118,144
  • 57
  • 340
  • 684