Very noob array question
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.
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.
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.
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 ...
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 ...
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 );
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 );
malloc is c, the c++ verson of that is new and delete which you use to allocate variable memory dynamical.
Quote:Original post by ricekrispywi dont think anyone really answered your question completly, so here's some sample code:
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.
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;}
#include <vector>void foobar( int arraySize, int arrayResize ) { std::vector<int> myArray( arraySize ); // .... myArray.resize( arrayResize ); // ....}
Cheers
-Danu
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement