Skip to main content
gamedev.net
🔒 Locked

allocate v[][] dynamically?

Started by Nibbles Dec 11, 2002 at 10:58 AM 6 replies 200+ views
Original Post
Nibbles
Nibbles
Hi, I was wondering how to allocate memory for v[][] (or **v?) dynamially to (for example) v[100][100]. I''m not entirely sure if i''m even using the right terms for this. Thanks! Scott
------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
erjo
erjo
Hey.
To dynamically allocate memory for a 2d array(x[][]), youd use this:

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


[edited by - erjo on December 11, 2002 12:03:06 PM]
MrScruff
MrScruff
A couple of things here, firstly which language are you using? C or C++?

For C use malloc, (I''ve forgotten the implementation (haven''t used C for years!)

For C++:

float* v = new float[100*100];

will give you the memory, but don''t forget to free it!

delete[] v;

will accomplish this when you''ve finished with it. Don''t get it confused with:

delete v;

which won''t delete the array properly.
Qw3r7yU10p!
Qw3r7yU10p!
I guess you want to do it this way so that you have the convenience of the [4][9] subscript notation.

Probably a safer, cleaner way to do that is to make a class like this:


template<class Type>
class Matrix {
public:
Matrix(int rows, int columns);
Type& operator()(int row, int column);
private:
favouriteContainer<Type> data_;
};


then you can access it like this

Matrix<double> board(8, 8);
board(3, 7) = 16.3;

The container member data_ can hold rows*columns amount of data like MrScruff said
The operator() can translate the row and column into an index into your data.
Ask if you need anything claifying.

Peace

[edited by - petewood on December 11, 2002 12:32:48 PM]
Nibbles
Nibbles
thank guys, i appreciate the quick replies and i''ll see what i can do.

thanks again,
Scott


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
daerid
daerid
Probably the quickest way to do it is this:


template<class T>
class matrix : public std::vector<std::vector< T > >
{
public:
matrix(){}
matrix(size_t x,size_t y)
{
resize(x);
for(size_t i = 0; i < x; ++i)
(*this)[i].resize(y);
}
};


[edited by - daerid on December 11, 2002 1:46:23 PM]
daerid@gmail.com
Punty50
Punty50
Hey,
In our AP comp sci class we use apmatrix and apvector. I have included links to the source, and you might be able to get an idea of what is going on by looking through the code. Our matrix class is a vector of vectors, which is what daerid has shown. I personally have just used a line of memory and accessed it by pointer arithmitic, seems a bit easier with fewer dependant classes. Here are the links:

http://cs.columbusacademy.org/reference/code/apvectorh.html
http://cs.columbusacademy.org/reference/code/apvector.html
http://cs.columbusacademy.org/reference/code/apmatrixh.html
http://cs.columbusacademy.org/reference/code/apmatrix.html

the first two are the apvector code, with the .h and .cpp respectively, the second is apmatrix with the .h and .cpp respectively.


Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.