Jump to content
  • Advertisement
Sign in to follow this  
nife

Resize array problem

This topic is 5095 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

Hi, I'm making a program which uses the A* search method, and therefore I've made a struct named Node, and a class named NodeList:
struct Node
{
	POINT parent, pos;
	int f, g, h;
};

class NodeList
{
	public:
		int count;
		Node *nodes;
		NodeList() {count = 0;};
		~NodeList() {delete [] nodes;};
		void Add(Node*);
		void Clear();
		void Delete(int);
		int Find(POINT*);
};

And then I have this little funtion which adds a Node to the list of nodes, but I can only execute the function Add(Node*) 8 times, before the program suddenly quits! No errors, no nothing, but I'm sure that something is wrong with the function, since the program doesn't quit after 8 tries if I don't use this function:
void NodeList::Add(Node *node)
{
	int i;
	Node *data, t;
	data = new Node[count];
	for (i = 0; i < count; i++)
	data = nodes;
	delete [] nodes;
	nodes = new Node[count + 1];
	for (i = 0; i < count; i++)
	nodes = data;
	delete [] data;
	nodes[count] = *node;
	count++;
}

Anyone who knows what's causing the error?

Share this post


Link to post
Share on other sites
Advertisement
Not at first glance, though I can tell that using a std::vector (or std::deque) would make your life infinitely easier.


#include <vector> // For std::vector
#include <iterator> // For std::distance

struct Node
{
POINT parent, pos;
int f, g, h;
};

class NodeList
{
public:
std::vector<Node> nodes;

NodeList() {}
~NodeList() {}

void Add(Node*);

void Clear();
void Delete(int);

int Find(POINT*);
};




void NodeList::Add(Node *node)
{
nodes.push_back(*node);
}

void NodeList::Clear()
{
nodes.clear();
}

void NodeList::Delete(int index)
{
nodes.erase(index);
}

int NodeList::Find(POINT* p)
{
// Should use find_if instead...
std::vector<Node>::iterator itor;
std::vector<Node>::iterator end = nodes.end();

for(itor = nodes.begin(); itor != end; ++itor)
if(itor->pos == *p)
return std::distance(nodes.begin(), itor);

// Should really throw an exception.
return -1;
}



In fact, you probably wouldn't need the NodeList class at all :)

Note -- for Find, you may be more familiar with code like this:


int NodeList::Find(POINT* p)
{
unsigned int index;
for(index=0; index < nodes.size(); ++index)
if(nodes[index].pos == *p)
return index;

return -1;
}



Watch out for the unsigned/signed conversion. An exception would be a better solution. And, as I noted in the code, using std::find_if would be more idiomatic.

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!