17

I understand that this question was asked before but I don't get why it doesn't work in my case

void calc(vector<char> zodis1, vector<char> zodis2, vector<char> zodisAts,int zo1,int zo2,int zoA)
{
   int i,u=0;

   int zod1[zo1]=0;
   int zod2[zo2]=0;
   int zodA[zoA]=0; 
}

All 3 of zod1, zod2, zoA gives me error: variable-sized object may not be initialized c++ But compiler should know the meaning of zo before initialization cause cout<<zo1; works and print out the meaning

So whats the problem?

Rakib
  • 7,435
  • 7
  • 29
  • 45
user3102621
  • 425
  • 2
  • 4
  • 9
  • 2
    To be clear: in standard C++ `int zod1[zo1];` is not permitted. However, many compilers offer that as an extension. Judging by your compiler's error message, it has an extension to allow `int zod1[zo1];` but not an extension to allow supplying the `=0;` to that. – M.M May 20 '14 at 04:53

2 Answers2

24

You can declare an array only with constant size, which can be deduced at compile time. zo1,zo2 and zoA are variables, and the values can be known only at runtime.

To elaborate, when you allocate memory on the stack, the size must be known at compile time. Since the arrays are local to the method, they will be placed on the stack. You can either use constant value, or allocate memory in the heap using new, and deallocate when done using delete, like

int* zod1 = new int[zo1];
//.... other code


delete[] zod1;

But you can use vector instead of array here also, and vector will take care of allocation on the heap.

As a side note, you should not pass vector by value, as the whole vector will be copied and passed as argument, and no change will be visible at the caller side. Use vector<char>& zodis1 instead.

Alium Britt
  • 1,246
  • 4
  • 13
  • 25
Rakib
  • 7,435
  • 7
  • 29
  • 45
  • 2
    To elaborate: the variables can change value at any time, but the file needs to know how much memory to allocate. And to elaborate on the vector side note: This is called a 'pointer', essentially instead of copying a possibly large amount of memory, it just 'points' to the data; this is much more efficient. – lewisjb May 20 '14 at 04:39
  • So when the whole code is compiled I must know the size. It wont work if I try to give the size just before the part of the code is being run? – user3102621 May 20 '14 at 04:41
  • 1
    @user3102621, you can, when the allocation is on the heap, not on stack. – Rakib May 20 '14 at 04:42
  • thanks for the answer. On the vector side I guess that would be better but I wont be doing any changes to vectors, so there is no need to do so (except I think it would save memory?) – user3102621 May 20 '14 at 04:47
  • `vector zod1(zo1);` is a lot simpler, as there is no chance of failing to delete it aftewards. – M.M May 20 '14 at 04:50
  • @user3102621, if you don't need change, then send as constant `const vector&` – Rakib May 20 '14 at 04:51
  • @Pyro in the side note , it is a *reference*, not a pointer. It means that `zodis1` denotes the same vector as was supplied to the function call. – M.M May 20 '14 at 04:52
  • @Rakib, That's not entirely accurate. I _can_ declare an array with variable size, only I _can't_ initialize it. Sure, the assembly looks ugly, but apparently the compiler has a way of figuring the size of the array at run-time. The initialization is tricky, as there is no way to know, compile-time, that you're not introducing a bug by writing to addresses in the stack that you shouldn't. See also: [Why is variable sized object initialization not allowed using const](http://stackoverflow.com/questions/21145241/why-is-variable-sized-object-initialization-not-allowed-using-const). – mihai Jul 31 '15 at 09:20
0

Here is the fix, you can write the following lines instead of the line where you got the error;

Alternative 1 you can use vectors:

vector<int> zod1(zo1, 0);

Alternative 2 (for example, since w know "0 <= s.length <= 100", we can use constant value):

int zod1[100] = { 0 };