Advertisement

Declaring arrays of unknown size

Started by October 21, 2000 01:50 AM
2 comments, last by DeusMaximus 24 years, 3 months ago
I am in a rush so this question will be straight-foreward is there anyway do declare an array which is the size of a variable used in your program. I am asking this because i am making a permutation program (n choose k) where k=4 but i want the user to be able to input n. for example; int n; cout<<"Enter a value for n: "; cin>>n; char set[n]; I know this way won''t work, so is there even a way. Thanks in advance, deus
To do dynamic memory allocation in C++ use use the new and delete operators. For example, you can change your code to:

char *set = new char[n];

This tells the program to create an array on the heap of type char and length n. It then sets the "set" variibable to point to this memory. You must be careful, though, since you must also delete this memory at some later point before your program exits or you will have a memory leak. You do this with the command:

delete[] set;

This tells the program to delete the array (b/c of the brackets) that set points to. If you are unfamiliar with pointers and such, then it would be well worth your time to learn them very well right now. You can get started by looking at those "Teach yourself C++ is 21 days" tutorials that can be found online, or even better would be to pick up a good book on the C++ language that you can use as a reference. One of the best is Stroustrup''s (sp?) "The C++ Language" (I think its in 3rd edition.) This is the language reference straight from the inventors mouth... generally considered the definitive reference for most things C++
Advertisement
There are few ways to accomplish this, at least that I know of. The first is to use the vector class (C++) instead of a standard array. A full explanation of vectors is beyond the scope of one measly post so be sure to read a little about them but for simple programs they can be treated almost the same as arrays (ie, operator[] is defined for vectors). So in your example you would do:

#include

// ...

int n;
vector set;

cout << "Enter a value for n: ";
cin >> n;

// check for invalid sizes of n
set.resize(n);

// ...

Or (the second way):

#include

// ...

int n;

cout << "Enter a value for n: ";
cin >> n;

vector set(n);

// ...

The difference is just a design choice really but keep in mind the resize(int) method for vectors can be very costly (running-time wise) so choose the best for the situation.

The other way is to use a pointer to a dynamic array like so:

// ...

int n;
char *set;

cout << "Enter a value for n: ";
cin >> n;

// check for invalid n values (negative)
set = new char[n];

// ...

// somewhere before set goes out of scope
delete [] set;

// ...

This option is a little trickier but most would prefer to do it this way. The variable set is now a pointer to an array rather than the array itself, meaning the value of set is the address of the first array index in memory:

&set == &set[0]

Now set, where 0 <= i < n, can be used as a normal array would:

set[3] = ''t'';
set[5] = ''Z'';

When passing the array as a parameter in a function though there is a key difference. Normally when using a static array (char set[n]) one would do this:

void passMyArray(char[], int);

With a dynamically sized array the pointer to the array must be passed instead, like so:

void passMyDynamicArray(char*, int);

Other than that... Do a search on any term you don''t understand (ie, dynamic memory allocation, vector, etc.) and you should come up with plenty of explanation.
Okay um on the previous post the #include lines are supposed to include the vector class but for some reason the greater-than and less-than symbols didn''t show up correct. Lets try again:

#include < vector > // don''t do spaces

Also the line:

vector set;

Is supposed to be:

vector < char > set; // don''t do spaces, but they don''t hurt in this case

This topic is closed to new replies.

Advertisement