Advertisement

Dynamic class array allocation

Started by July 29, 2000 02:59 PM
10 comments, last by Possibility 24 years, 5 months ago
Hi, I have a class array and I need to dynamically allocate it using C++. What I currently have is something like this: class TILE { }; extern TILE tile[50][50]; but how can I make that size variable depending on the user input and still make it globaly available? I looked in my C/C++ book and didnt find squate on it. Possibility Edited by - Possibility on 7/29/00 3:00:51 PM
If I''m not mistaken, you can make a linked list with nodes.
Something like this:

class TILE
{

// Put whatever you want here
TILE *next; // A link to the next node in list
};

TILE* head=NULL, // Head of list
tail=NULL; // Tail of list

Now to add a new node:

TILE *new_node;
new_node = (TILE *)malloc(sizeof(TILE));
new_node->(some variable) = (some information);
And now you have to decide where to put it in the list.
I can''t put the source here since it''ll take up some space, try to find a good tutorial.
Or, you can do it the easy way and just allocate a fixed size of the array: extern TILE tile[100][100];
It''ll waste some memory, but it''s easier to use.




The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Advertisement
Wow, man, either I completely misunderstood your problem, or it is very simple. Ever heard about pointers? Ever heard about the free store? If not, you need to read a better C++ book.

class TILE
{
};
TILE * tile;

then create the array like this...
int x = 50;
int y = 50;
tile = new TILE[y][x];

When you no longer need the array, don''t forget to delete it:
delete[] tile;
Ah sweet, thanks mr. Anonymous, I knew it would be something easy like that

Possibility
That won''t work... You can''t have both dimensions dynamic and expect new to allocate it for you, since it requires the allocation of an array of pointers to handle both dimensions.

Here is the way to allocate the array dynamically:


TILE** array = new TILE*[width];

for( int x=0; x < height; x++ )
array[x] = new TILE[height];



Then to delete it, you do this:


for( int x=0; x < height; x++ )
delete[] array[x];

delete[] array;



Basically, you allocate an array of pointers, and then each pointer contains an array. And that''s a multi-dimensional array.



- null_pointer
Sabre Multimedia
quote:
int x = 50;
int y = 50;
tile = new TILE[y][x];


That wont work, you can''t create a 2d array dynamically like this - only the last (or is it the first?) size can be dynamic. Since your using C++, you already have a very good dynamic array class. It''s called std::vector. Have a look at this thread for more info (scroll down to my post).
Advertisement
Ok, neither of them methods seemed to work. I tried will Wilka's method as so:
	int dimension1 = 100; // size of first dimension you want		int dimension2 = 100; // size of second dimension you want		typedef vector TILE_vector;		typedef vector TILE_vector2D;			TILE_vector2D tile(dimension1, TILE_vector(dimension2));  


and upon running the program I emmediately get a error box:

Microsoft Visual C++ Debug Library
Debug Assertion Failed!

File dbgheap.c
ect...

so I am totaly confused on this dynamic class array stuff. So here is what I was doing initially before asking on gamedev

        TILE *tile[100][100];    //this is globaly availablefor (int p = 0; p < 100; p++)   for (int q = 0; q < 100; q++)       {	  tile[p][q] = new TILE(D3DVECTOR(planetary_radius * cosf(angle),	// X position		0.0f + 32.0f*row,  // Y position		planetary_radius * sinf(angle)),// Z position		D3DVECTOR(64.0f, 64.0f, 0.0f),// The dimensions of the tile                0.95f, 1.0f, 0.8f);// R G B values       }    

but as you can see, I cant declare TILE *tile[user_input_x][user_input_y] in the global space dynamically

Possibility


Edited by - Possibility on July 29, 2000 9:06:03 PM
I had that problem a few weeks ago, and i implemented a
simple array class:

class C2DArray
{
void *m_Ptr;
long m_Width;
long m_Height;
long m_ElementSize;

public:

C2DArray()
{
m_Ptr = 0;
m_Width = 0;
m_Height = 0;
}

~C2DArray()
{
if (m_Ptr)
{
free(m_Ptr)
m_Ptr = 0;
}
m_Width = 0;
m_Height = 0;
}

bool Create(long Width, long Height, long ElementSize)
{
if (Width < 0 || Height < 0 || ElementSize < 0)
return false;

m_Width = Width;
m_Height = Height;
m_ElementSize = ElementSize;

if (m_Ptr)
free(m_Ptr);

m_Ptr = (void *)malloc(m_Width * m_Height * m_ElementSize);
if (!m_Ptr)
return false;

return true;
}

bool SetElement(long x, long y, void *Element)
{
if (x < 0 || x >= m_Width)
return false;

if (y < 0 || y >= m_Height)
return false;

if (!m_Ptr)
return false;

memcpy(m_Ptr + (y * m_Width + x) * m_ElementSize, Element, m_ElementSize);

return true;
}

bool GetElement(long x, long y, void *Element)
{
if (x < 0 || x >= m_Width)
return false;

if (y < 0 || y >= m_Height)
return false;

if (!m_Ptr)
return false;

memcpy(Element, m_Ptr + (y * m_Width + x) * m_ElementSize, m_ElementSize);

return true;
}
}

So you can simply do the following:

C2DArray A;
int i = 4;
A.Create(100, 100, sizeof(int));
A.SetElement(5, 5, &i);

Well i have to admit that this isn''t the class i''m using.
I wrote this just for you So please forgive me any errors...
Possibility:

Are you sure that you typed in the code that I posted in exactly the same way?

If you want to use the STL class (very good btw), then I think you need to use this code:


vector< vector< TILE > > tiles;

tiles.reserve(first_dimension);

for( unsigned int x=0; x < second_dimension; x++ )
{
tiles[x].reserve(second_dimension);
}



And "first_dimension" and "second_dimension" are the amounts of tiles of the first and second dimensions, respectively.


- null_pointer
Sabre Multimedia
null_pointer''s method should work.

It should also be part of a FAQ or something. This exact question is asked & answered so much here. Problem is I don''t think anybody reads the FAQs (I know I never do. )

This topic is closed to new replies.

Advertisement