Hello all

I have just started to learn programming, and I am having a little trouble getting around array and vectors; I need to create a structure to store a certain amount of data based on the user's input. I was told by my teacher that I have to declare the size of the array right from the start, and if I wanted to create an array of various size during different runs of the program I should use vector to dynamically allocate memory.My question is could I just ask the user for size, store the into a variable, then declare the size of an array using that variable? Hope I am making sense. Thank you in advance.

Regards

Douglas

You can declare an array dynamically: see this for a good example. However, if you have been taught vectors there is virtually no reason not to use them (I could see a case where you had a requirement for very very low overhead). The vector will expand automatically if the need for more storage arises, so you can pack stuff into it for quite a while before you even had to think about it.

You can't just ask the user to enter the size, normally you set the size as a constant integer else you can create a pointer like the following:

(suppose you need an array of charcters )

char *name= new char;
//the new char is doing the dynamic memory allocation

Post #2 of this thread contains an algorithm you will need if you want to use a dynamic-sized array instead of a vector. Reallocating memory to expand the array's size can be a little complicated to understand for new programmers, so using a vector would be ideal.

Usually, starters start up using regular arrays, because it easy, plus
usually you don't need to worry about efficiency, and or resizing the array.

But once, you get the idea about arrays, you might want to move up with vectors, and understand how to use them. They are just like
arrays, but you don't have to worry about its complications. All you
need to know is how to use the interface it provides. The other
difference is the initialization part.

>> My question is could I just ask the user for size, store the into a variable, then declare the size of an array using that variable

Yes, you have to use pointers :

unsigned int SIZE = 0;
int *Array = new int[SIZE];
//use it like regular arrays
A[0] = 1;
A[1] = 3;

//after done with using the Array, you need to delete the array
delete [] Array;

Modern C++ beginner books start out by introducing vectors early on, sometimes without mentioning arrays at all except for the final chapters where they're discussed in among the 'C' subset of the language.

They do this for a good reason; C++ has been deliberately designed with the aim that programmers should never need to use arrays; the STL which all but replaces arrays is easy to learn, easy to use, safer, and usually more efficient. (Despite this, there are still a few odd situations where arrays are the best solution to a problem, but those are less common.).

If you're still in the early learning phase, skip arrays for now and focus on the STL (Hopefully you have a modern book which does this too). You lose nothing by learning how to use vectors, iterators and algorithms, and hopefully gain alot more in the time you spend than if you were to spend alot of time working with arrays.
You can go back to arrays later on when you're more comfortable with the basic parts of the STL, and you'll be in a far better position to see for yourself why arrays don't get used much.

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