Very noob array question

Started by
6 comments, last by ricekrispyw 19 years ago
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.
The best way to predict the future is to invent it.
Advertisement
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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 ...
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 );


malloc is c, the c++ verson of that is new and delete which you use to allocate variable memory dynamical.
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
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Thank you all. You're great.
The best way to predict the future is to invent it.

This topic is closed to new replies.

Advertisement