18

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?

1
  • 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, 2014 at 4:53

2 Answers 2

27

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.

8
  • 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, 2014 at 4: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? May 20, 2014 at 4:41
  • 1
    @user3102621, you can, when the allocation is on the heap, not on stack.
    – Rakib
    May 20, 2014 at 4: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?) May 20, 2014 at 4:47
  • vector<int> zod1(zo1); is a lot simpler, as there is no chance of failing to delete it aftewards.
    – M.M
    May 20, 2014 at 4:50
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 };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.