Sign in to follow this  
ricekrispyw

Very noob array question

Recommended Posts

ricekrispyw    388
How do I make an array of variable size? Not dynamic size, but say I have variable x, which is equal to 7. How can I simulate: myArr = new int[x]; I know this is very noob, but hey, that's what the board is for. Thanks in advance.

Share this post


Link to post
Share on other sites
smart_idiot    1298
Variable length arrays aren't standard in C++. GCC does have them, you can simply write int myArr[x];, but it's a bad idea if you want to write portable code. You're better off either allocating a chunk of memory or using a container such as a vector.

Share this post


Link to post
Share on other sites
Bellini    122
The easiest way I found to do this is to use the "vector" template class. Its part of the standard library for C++. It allows you to start with a given size, then you can resize it when you need to (smaller or bigger). You can added to the begining or end, etc.

Share this post


Link to post
Share on other sites
Xai    1848
your initial post is the ONLY way to do it in C/C++. But your terminology is half right, half backwards ...

you cannot make a variable sized array in C/C++, period. Meaning, an array that changes size (an array who's size is variable).

you can make an array at run time with the new operator (as you showed in your original post). This is called a dynamicly created array, and it have a size based on the current value of a variable, but will thereafter have a static size.

Therefore there are no variable or dynamically sized arrays in C/C++. Just dynamically created arrays with fixed size.

...

as mentioned by others, the C++ standard class vector is a class which implements a dynamically / variably sized array ...

Share this post


Link to post
Share on other sites
boohillie    210
Couldn't you "Simulate it" using malloc()?

example:

int *pMyArray = NULL;
pMyArray = (int*)malloc( (int)(sizeof(int) * x) );

then just use pMyArray like any other array and when you're done
free( pMyArray );


Share this post


Link to post
Share on other sites
silvermace    634
Quote:
Original post by ricekrispyw
How do I make an array of variable size? Not dynamic size, but say I have variable x, which is equal to 7. How can I simulate:

myArr = new int[x];

I know this is very noob, but hey, that's what the board is for. Thanks in advance.
i dont think anyone really answered your question completly, so here's some sample code:
void foo( int arraySize ) {
int *myArray = new int[ arraySize ];
// ....
delete [] myArray;
}

in C++, to resize an array, you must delete [] it and then new [] it again:
void bar( int arraySize, int arrayResize ) {
int *myArray = new int[ arraySize ];
// ....
delete [] myArray;

myArray = new int[ arrayResize ];
// ....
delete [] myArray;
}
the other way, if you are comfortable with the standard C++ libraries and have a general understanding of C++ templates or Java 1.5 generics:
#include <vector>
void foobar( int arraySize, int arrayResize ) {
std::vector<int> myArray( arraySize );
// ....
myArray.resize( arrayResize );
// ....
}
as you can see, the std::vector way is pretty sweet looking, and i would advice that the most, as you will need to learn them at some stage anyway.

Cheers
-Danu

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this