Dynamic Arrays

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Nova
Competition winner
Posts: 99
Joined: Mon May 09, 2005 10:32 am

Dynamic Arrays

Post by Nova »

Hey everyone,

I am searching for a way to dynamically change the size of an array during runtime.
I will create an example to explain what I want to do:

Let's say we have a class (partly pseudo code)

Code: Select all

class MyClass {
private:
  <array of integer> walkmap
  void CreateMap(int map);
};
walkmap is supposed to be a 2d array of integers that holds diffrent values for different map types [0=not walkable,1=walkable], so the AI could access this walkmap during its pathfinding algorythms

Of course different maps have different walking abilities, therefore I have created the CreateMap() function


My problem is that different maps, too have different sizes:

Code: Select all

void MyClass::CreateMap(int map) {
	switch(map) {
		case 1:
		{
			walkmap[0] = 0 0 0 1 1 1 0 0 0;
			walkmap[1] = 0 0 1 1 1 0 0 1 1;
			walkmap[2] = 0 0 1 1 0 0 1 1 0;
			walkmap[3] = 0 0 0 1 1 1 1 0 0;
		}
		case 2:
		{
			walkmap[0] = 0 0 0 1 1 1 1 1 1 1 1 1 1 1;
			walkmap[1] = 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
		}
	}
}
So how can I make the different walkmaps but still use the walkmap variable?

thnx in advance
-Nova
Xico
Posts: 30
Joined: Sun Jun 05, 2005 5:08 pm
Location: Buenos Aires, Argentina
Contact:

std::vector

Post by Xico »

Using vectors as container instead of arrays?
here info about vector and other container classes: http://www.roguewave.com/support/docs/s ... f/2-7.html
hope it help
regards
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

you could use irrlicht's inbuilt arrays, they handle their own memory allocation but I think they are limited to one dimension-

Code: Select all

core::array<int> map;
// then...
if ( map.size() < width * height) 
  map.set_used(width * height);
map[x+(y*width)] = whatever;
i found that irrlicht's arrays are slow when you heavily use [] operators inside loops, so use the array class for its memory management but read and write the data manually using good old c pointers. eg-

Code: Select all

int *arraydata;
core::array<int> map;

map.set_used(100);  // grow the array
arraydata = map.pointer(); // get memory location

// 
for (int i = 0; i<100; i++) 
{
    // "map[i]" means call core::array's [] function to get the int
    // "arraydata[i]" means "an integer at location arraydata+(sizeof(int)*i)"
    arraydata [i] = somecalculation(i);
}
Masdus
Posts: 186
Joined: Tue Aug 26, 2003 1:13 pm
Location: Australia

Post by Masdus »

Its to dynamically resize c++ arrays, i remember doing in back in 2nd year Uni. Problem is i can't remeber how its done. I'm sure a google search will turn up some example code,
bearSoft
Posts: 165
Joined: Fri Apr 01, 2005 9:55 pm
Location: Denmark

Post by bearSoft »

u need to allocate heap memory using the 'new' operaor
eg
int *vek = new int[n][m];

when not needed anymore remember to free the heap
delete [] vek;

it is also posible to build a template class so that different types can be stored in the same vektor, however big isues vith the operators emerges
Regards.
Tech: win98se| 320mb ram| abitbe6| 433mhzceleron| atiRadeon7000.64mb| soundblaster125| dx9.0b | devCPP | IRR 0.12.0 |
Post Reply