# Pointer arrays with new

## Recommended Posts

So here is what I want: int* vertices; vertices = new int(100); I think this is how to create 100 new ints in memory.

##### Share on other sites
raz0r    586
You must use operator new[].
For example, int * piData = new int[100](); //zeros.
Don't forget to delete[] your data when you're done with it...

##### Share on other sites
I'm keeping the data actually for models, so it stays :). But yea I just figured out before you replied that i need []. Thanks.

##### Share on other sites
When he said use delete[], he meant when you are done using the variable. The new operator allocates memory and does not get rid of it like a normal variable declaration. So when your program is closing, use delete[].

##### Share on other sites
I actually have a new question. Im using a GNU compiler and maybe this is a GNU thing but can I make the array multidimensional?

int *vertices = new int [100*3]; // would like, int *vertices = new int [100][3];

error>> vertices[i][j];

##### Share on other sites
Zerd    122
If you use a one dimensional array you have to access this also like a one dimensional array:

verticles[row*column];

or like this
class Simple2DimArr{    private:        int* pArr;        int Rows, Cols;    public:        Simple2DimArr(int Zeilen, int Spalten)        {            Rows = Zeilen;            Cols = Spalten;            pArr = new int[Rows * Cols];        }        int* operator[](int Zeile)        {            return & pArr[Zeile * Cols];        }        ~Simple2DimArr()        {            delete [] pArr;        }};int main(){    Simple2DimArr Arr(2,3);    Arr[0][0] = 42;}

##### Share on other sites
Sc4Freak    643
Quote:
 Original post by dpadam450I actually have a new question. Im using a GNU compiler and maybe this is a GNU thing but can I make the array multidimensional?int *vertices = new int [100*3]; // would like, int *vertices = new int [100][3];error>> vertices[i][j];

I don't think you can create a dynamically allocated multidimensional array like that, probably because the compiler needs to know at compile time what the length of a row in your array is. However, you can dynamically allocate an array of pointers, and allocate the individual rows of memory and store into those pointers.

int** Arr;*Arr = new int [100];for(int i = 0 ; i < 100 ; i++)    Arr[i] = new int [100];

You should be able to access Arr like any other multidimensional array, eg. Arr[5][6]

##### Share on other sites
Guest Anonymous Poster
That should be:

int **Arr = new int*[3];for(int i = 0 ; i < 3 ; i++)	Arr[i] = new int [3];// you can now access the elements with Arr[x][y]	for(int i = 0 ; i < 3 ; i++)	delete[] Arr[i];delete[] Arr;

Multi-dimensional arrays in C++ are really just arrays of arrays, and they're more trouble than they're worth. Consider just sticking to a 1D array to represent 2D data:

int *Arr = new int[w*h];// access the elements with Arr[x + y*w]delete[] Arr;

##### Share on other sites
Captain P    1092
Why would you use ints for model vertices? I would assume you need some more precision than that.

Anyway, write a Vertex class or struct that contains 3 floats (x, y and z), rather than using a 2D array. You'd then use an array or std::vector (which I would recommend) of Vertex objects or pointers to them. It's more solid, especially since you can give your Vertex class additional functionality, which could come in handy. Plus, it expresses better what you're doing. It's easier to understand if you come back to the code a few months later, and if done well, it also makes developing easier.

##### Share on other sites
Quote:
 Original post by Captain PWhy would you use ints for model vertices? I would assume you need some more precision than that.

Although you're probably right in that floats are the most appropriate general case vertex format, it's worth pointing out that almost all object & character models can use 16-bit ints ("short" type under VC++) for vertex position data without any precision problems. You need to scale the resulting model back to whatever the appropriate size is. I've just used a single scalar component per axis when I've done this.

##### Share on other sites
Zahlman    1682
0) For Beginners. Sorry if I deflate your ego.

1) Use std::vector if you don't know the size ahead of time. If you do, then why not just stack-allocate it (i.e. 'int foo[100];')? (If there is a set size but you also have to pass the object around, use boost::array, or wrap it in a struct of your own.)

2) Use boost::multi_array if you don't know the size ahead of time and you also want the array to be multi-dimensional. If you know the size of each dimension, you can make a boost::array of boost::arrays, for example. Vector of boost::array, and boost::array of vector are also both perfectly kosher; but keep in mind that the latter is not necessarily "rectangular" - go back to multi_array if you need that. You can also make a vector of vectors, if you specifically want it not to be rectangular (that is, an unknown-sized set of things, each of which things is itself an unknown-sized (independant sizes) set of ints).