Pointer arrays with new

Started by
9 comments, last by Zahlman 17 years, 4 months ago
So here is what I want: int* vertices; vertices = new int(100); I think this is how to create 100 new ints in memory.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Advertisement
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...
I'm keeping the data actually for models, so it stays :). But yea I just figured out before you replied that i need []. Thanks.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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[].
bi-FreeSoftware
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[j];

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

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;}
Quote:Original post by dpadam450
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[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 = new int [100];


You should be able to access Arr like any other multidimensional array, eg. Arr[5][6]
NextWar: The Quest for Earth available now for Windows Phone 7.
That should be:

int **Arr = new int*[3];for(int i = 0 ; i < 3 ; i++)	Arr = new int [3];// you can now access the elements with Arr[x][y]	for(int i = 0 ; i < 3 ; i++)	delete[] Arr;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;
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.
Create-ivity - a game development blog Mouseover for more information.
Quote:Original post by Captain P
Why 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.

This topic is closed to new replies.

Advertisement