Hi there, I am very new to C++ and I want to create an array of variable length.
I tried this:

int corners;
	cout << "Please enter the number of corners: ";
	cin >> corners;
	int x[corners];

but it came up with errors:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'x' : unknown size

After some research I found this as a solution:

int corners;
	cout << "Please enter the number of corners: ";
	cin >> corners;
	int * const x = (int*)_alloca(corners * sizeof(int));

Whilst this does work, I was hoping someone could explain exactly what it meant/was doing.

I am using Visual C++ Express 2008.

Thanks very much for any help! :)

Don't do it -- it's a local extension and not part of C++. Do this instead:

int corners;
cout << "Please enter the number of corners: ";	cin >> corners;
vector<int> x(corners);    // Note: parentheses, not square brackets

You have to say

#include <vector>

for this to work.

Thanks arkoenig!

I can see why this is a better solution. I have looked up the vector function and I can see why it would be good to do what I want. However, I can't fully get my brain around what the statment

vector<int> x(corners)

is doing.

Sorry for being a bit slow!

It's defining a variable named x of type vector<int> and using the value of corners to initialize it. When you use an integer to initialize a vector, that integer determines the number of elements the vector has.

By the way, many people who use vectors also use the push_back function. For example:

vector<int> x;
int n;
while (cin >> n)
    x.push_back(n);

Calling x.push_back(n) pushes a copy of n onto the back of x. That is, it increases the size of x by 1 and uses n to initialize the newly created element.

This technique makes it unnecessary to know in advance how many elements the vector will have.

A vector is based on a C++ system called a "template". You don't have to worry about exactly what a template is and how they work at this point, I'm sure you'll learn soon enough. In absolute simplest terms, a template allows you to define a class or function one (1) time and make it work for virtually any dataType without having to re-write it.

A vector's declaration includes this dataType as what is known as a "template argument" (the part between '<' and '>'). Thus, the general form of the declaration of a vector can be described as:

vector< elementDataType > vectorName(vectorSize)

"I want to create a vector of elementDataType objects. I want to call the vector "vectorName" and it will have "vectorSize" elements in it."

EDIT:
Oops... What arkoenig said too...
I really didn't want to mention it to prevent confusion, but there are actually several ways of defining a vector. The methods mentioned in the thread are probably the 2 most-common ways.

Edited 6 Years Ago by Fbody: n/a

Thanks to both of you for your help. The push.back function would actually be very handy for me and would make me code much neater and more logical!

This article has been dead for over six months. Start a new discussion instead.