I am trying to implement a search algorithm,it needs a tree with four(NOTE n=4) nodes.Is the struct declaration correct for array of structure pointers??You see the main idea is start with the 'cur' node.Then take a new 'temp' node. Copy the contents of 'cur->state' into 'temp->state' then perform the 'res' operation.The result of which is stored in the 'cur->action[i]' node for every 'n' iteration.I am getting a memory leak ,I can only do one iteration.The program terminates at the 'temp' declaration during the second iteration.Please help me am I missing something??I have allocated and freed 'temp' then why the memory leak??Please ignore the other parts.
void srch(void *states,int nstates,int *actions,const int n,void (*res)(void *,int),unsigned long int (*heuc)(void *),bool (*gl)(void *))
{
typedef struct tree
{
void *state;
tree *action[4];
};
tree *cur=new tree;
for(int i=0;i<n;i++)
{
cur->action[i]=NULL;
}
while(!(*gl)(cur->state))
{
int ph=0,max=0;
for(int i=0;i<n;i++)
{
tree *temp=new tree;
memcpy( temp->state,cur->state,sizeof(cur->state)*nstates);
for(int k=0;k<n;k++)
temp->action[k]=NULL;
(*res)(temp->state,actions[i]);
cur->action[i]=temp;
memcpy(cur->action[i],temp,sizeof(tree*));
delete[] temp;
}
cur= cur->action[max];
}
delete[] cur;
}