-1

Visual Studio is giving error while compiling this c++ code. It says that size should be a constant variable. I have tried making it constant but it does not work.

int size;
cout << "Please Enter the array size : " ;
cin >> size;

int myArr[size];
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • 1
    You don't want to write code like this - it's a misfeature of C that g++ makes the default, but VisualC++ (correctly) does not. –  Sep 29 '18 at 22:42
  • You might look at [`alloca`](http://man7.org/linux/man-pages/man3/alloca.3.html) / [`_alloca`](https://msdn.microsoft.com/en-us/library/wb1s57t5.aspx), which have the same effect. – Paul Sanders Sep 29 '18 at 22:53
  • @Paul alloca is not part of C++. –  Sep 29 '18 at 23:07
  • @NeilButterworth I am aware, but the 'big 3' do support it. – Paul Sanders Sep 30 '18 at 10:45

1 Answers1

0

The size of an array must be known at compile time. In your case, the size is known at runtime so you must allocate the array from the heap.

int size;
std::cin >> size;

int* myArr = new int[size];
// ...
delete[] myArr;
Brady Dean
  • 3,378
  • 5
  • 24
  • 50
  • But don't do this - use a std::vector. –  Sep 29 '18 at 22:41
  • @NeilButterworth I know people make this recommendation because newbies forget to deallocate, but some algorithms just need a fixed array and don't want to copy elements around during a resize. – Brady Dean Sep 29 '18 at 22:50
  • 1
    It's not because of the memory management, it's because it makes life so much easier - for example, passing things to functions. And you can create a fixed-sized vector at construction-time. The need to do that is somewhat rare, but you can do it. I doubt very much the OP has this issue. –  Sep 29 '18 at 22:53
  • @BradyDean *"some algorithms just need a fixed array and don't want to copy elements around during a resize"* If the size is fixed, then resize will never happen, no? You can choose a size when constructing the vector. – HolyBlackCat Sep 29 '18 at 22:56
  • 2
    If you really don't want `std::vector` (e.g. because there is no simple way to resize it without initializing the new elements, or because it's slightly larger than a plain pointer), then use `std::unique_ptr`. – HolyBlackCat Sep 29 '18 at 22:58
  • @HolyBlackCat Imagine an algorithm that inserts up to n elements into a stack. It would suffice to implement the stack using an array rather than a vector. – Brady Dean Sep 29 '18 at 23:00
  • 1
    @BradyDean As I said, in that case you can use `auto array = std::make_unique(n);`. Under the hood it's no different from manually using `new/delete`, *except* you can't forget to delete the memory. Also you get exception safety: memory is freed even if something throws. On the other hand, with manual `new/delete` calls there is no sane way to work with exceptions without getting leaks. – HolyBlackCat Sep 29 '18 at 23:03
  • 1
    @Holy What is the advantage of that over using a vector? –  Sep 29 '18 at 23:09
  • @NeilButterworth if you need to push 10000 elements into a vector it may need to reallocate multiple times. Or you could just allocate it all at one with a unique pointer array. – Brady Dean Sep 29 '18 at 23:11
  • @Brady As people keep pointing out to you, you can size a vector at construction time. just like an array. For example `vector v (1000);` –  Sep 29 '18 at 23:12
  • @NeilButterworth if the vector allocates everything up front, there is no benefit to using the vector over an array. Might as well use the right data structure for the right job – Brady Dean Sep 29 '18 at 23:15
  • @Brady As I pointed out but you don't seem to understand, vectors are much, much easier to use than arrays, primarily because they carry their size around with them. And ease of use trumps all other factors, except in a marginal number of cases. Anyway, enough. –  Sep 29 '18 at 23:18
  • @NeilButterworth imagine you have a class with a vector that allocates all of the memory it will use up front. Now imagine you save pointers into this vector for later use. The last thing you want is for someone else to modify your code and push into the vector. If it resizes everything breaks essentially. – Brady Dean Sep 29 '18 at 23:21