Jump to content
  • Advertisement
Sign in to follow this  
dpadam450

Pointer arrays with new

This topic is 4252 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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[j];

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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;

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!