Why can't I do something like this:
int size = menu.size;
int list[size];
Is there anyway around this instead of using a vector? (arrays are faster, so I wanted to use arrays)
thanks
The size must be known at compile-time, since the compiler needs to know how much stack space will be needed to allocate enough memory for it. (Edit: I stand corrected. In C, variable length arrays can be allocated on the stack. C++ does not allow variable length arrays, however.)
But, you can create arrays on the heap at run-time:
int* list = new int[size];
Just make sure you free the memory when you're done, or you'll get a memory leak:
delete [] list;
Note that it's very easy to accidentally create memory leaks, and a vector is almost for sure easier to use and maintain. Vectors are quite fast (especially if you reserve() them to the right size first), and I strongly recommend using a vector instead of manual memory-management.
In general, it's a good idea to profile your code to find out where the real bottlenecks are than to micro-optimize up front (because the optimizations are not always optimizations).
As other have said, the C++ language designers have chosen not to allow variable length arrays, VLAs, in spite of them being available in C99. However, if you are prepared to do a bit more work yourself, and you are simply desperate to allocate memory on the stack, you can use alloca().
That said, I personally would use std::vector. It is simpler, safer, more maintainable and likely fast enough.
C++ does not allow variable length arrays. The size must be known at compile-time. So you can't do that.
You can either use vectors or new.
vector<int> list;
or
int *list = new int[size];
If you go with the latter, you need to free it later on:
delete[] list;
arrays are faster, so I wanted to use arrays
This is not true. Where did you hear this from?
In C++, the length of an array needs to be known at compile time, in order to allocate it on the stack.
If you need to allocate an array whose size you do not know at compile time, you'll need to allocate it on the heap, using operator new[]
int size = menu.size;
int *list = new int[size];
But since you've new'd memory on the heap, you need to ensure you properly delete it when you are done with it.
delete[] list;
first of all, vectors aren't significantly faster. as for the reason why you cant do something like this:
the code will allocate the array on the stack. compilers have to know this size upfront so they can account for it. hence you can only use constants with that syntax.
an easy way around is creating the array on the heap: int list = new int[size]; Don't forget to delete[] later on.
However, if you use a vector and reserve the correct size upfront + compile with optimization, there should be little, to absolutely no overhead.
int a[n] where n is declared via const, then you actually get a VLA, i.e. the stack allocation is reserved at runtime rather than compile time. This is because C const only means read-only rather than constant.Since list is allocated on the stack, its size has to be known at compile time. But here, size is not known until runtime, so the size of list is not known at compile time.
list is allocated on the stack. The issue is that the C++ language does not support VLAs. It could if the language committee decided they wanted to do that. So your answer shouldn't talk about size being known at compile time. It should just say that you cannae dae this in C++.
vectorwould cause a bottleneck in your code. As a beginner you really should avoid making incorrect assumptions about the performance of your code. The vast majority of the time you will never see a performance difference and you avoid various types of bugs associated with using raw arrays.