Sign in to follow this  

Resize array problem

This topic is 4914 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[i] = nodes[i];
	delete [] nodes;
	nodes = new Node[count + 1];
	for (i = 0; i < count; i++)
	nodes[i] = data[i];
	delete [] data;
	nodes[count] = *node;
	count++;
}

Anyone who knows what's causing the error?

Share this post


Link to post
Share on other sites
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

This topic is 4914 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.

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