Sign in to follow this  
dpadam450

Pointer arrays with new

Recommended Posts

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


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


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


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


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


Link to post
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 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
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this