10

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

3
  • 13
    (arrays are faster, so I wanted to use arrays) - Please tell us under what circumstances arrays are faster and how using a vector would 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. Commented Oct 29, 2011 at 18:26
  • 1
    if you are worried about performance between vector and array you are barking up the wrong tree. much bigger performance sinks are in 99.9% of the cases dependent on the algorithm or i/o. Commented Oct 29, 2011 at 18:37
  • Yeah these guys are right. I use to think the same as you. I use to try and implement my own dynamic arrays when i started c++. Now i have realised that i was basically doing the same thing as a vector and the vector implementation is probably faster and safer than mine. Not to say you shouldn't play with arrays on the heap for learning purposes though. You will get a better understanding of how things work if you try making it yourself. Commented Mar 12, 2015 at 14:25

7 Answers 7

14

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Again, if your first paragraph was true then VLAs could not exist in C.
I know my list wouldn't get very big, but isn't storing something on the heap is slow aswell as they have to go to memory to retrieve it?(just so I know in future). Thanks
@Danny: No matter what you do, the elements you want to store are going to end up in memory (whether stack or heap). It won't be any faster to access them from the stack (barring caching issues). The heap requires a little more bookkeeping, so it may be a little slower to acquire the memory, but insignificantly so for most applications. If declaring memory on the heap becomes your bottleneck, then you can implement some sort of array reuse mechanism (or use a specialized allocator)
6

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.

2 Comments

+1 alloca() is the answer, if you really, truly want to allocate dynamically sized arrays on the stack at runtime in C++. But watch out for.... Stack Overflow! gasp
Or use VLAs with a compiler extension (gcc).
4

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?

Comments

2

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;

1 Comment

See my comments to the other questions - your first paragraph is excessively decisive. It is possible for systems to be made where stack memory allocation sizes can be determined at runtime.
2

AFAIK In C++03, there are no variable-length-arrays (VLA):

you probably want to do this:

const int size = menu.size;
int list[size]; // size is compile-time constant

or

int *list = new int[size]; // creates an array at runtime;
delete[] list;             // size can be non-const

Comments

1

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.

1 Comment

One interesting aspect of this in C is that if you write 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.
0

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.

3 Comments

If that was true then VLAs would not exist in C.
It's true in this case, because it's C++, not C.
The point I am making is that the issue is not that 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++.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.