Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.27/5 (3 votes)
i got this code, but i do not understand how to get rid of the error occurring at the malloc function.

C++
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
int initial[9]={2,8,3,1,6,4,7,0,5};    //the initial and final states
int final[9]={1,2,3,4,5,6,7,8,0};   
									
int moves[9][4]={                      
		{1,3,-1,-1},             
		{0,2,4,-1},
		{1,5,-1,-1},
		{4,0,6,-1},
		{3,1,5,7},
		{2,4,8,-1},
		{3,7,-1,-1},
		{4,6,8,-1},
		{5,7,-1,-1}
	  };
struct qtype  //queue
{
	int board[9];
	struct qtype *next;
	struct qtype *parent;
}*print;

struct node  //linked_list
{
	   int check[9];
	   struct node *next;
}*list,*p;

typedef struct qtype QUEUE;
typedef struct node NODE;

QUEUE *rear=NULL,*tfront=NULL;

		 int qempty()
		 { return tfront==NULL; }

void qinsert(int *m)
{
	int i;
	QUEUE *newnode;
	newnode=(QUEUE *)malloc(sizeof(QUEUE));
	for(i=0;i<9;i++)
		newnode->board[i]=m[i];
	newnode->parent=tfront; //parent
	if(rear==NULL)
	  rear=tfront=newnode;
	else
	  {
	  rear->next=newnode;
	  rear=newnode;
	  }
}

NODE * getnode(int *m)
{
	 NODE *tempnode;
	 int i;
	 tempnode=(NODE *)malloc(sizeof(NODE));
	 tempnode->next=NULL;
	 for(i=0;i<9;i++)
	tempnode->check[i]=m[i];
	 return tempnode;
}

void ins_in2_list(int *m)
{

	NODE *aux;
	int i;
	aux=getnode(m);
	if(list==NULL)
	   list=aux;

	else
	  {
	aux->next=list;
	list=aux;
	  }
}
			   int dublicate(int *m)
			   {
				   NODE *temp=list;
				   int i;
				   while(temp!=NULL)
				   {
				   for(i=0;i<9;i++)
					   {	  if(temp->check[i]!=m[i])
					  break;
					   }
					  if(i==9)
					 return 1;
					  temp=temp->next;
				   }
				   return 0;
			   }



void print_soln()
{
	int i;
	while(p!=NULL)
	{
	   for(i=0;i<9;i++)
	   {
	   printf("%d\t",p->check[i]);
	   if((i+1)%3==0)
	   printf("\n");
	   }
	   printf("\n\n");
	   getch();
	   p=p->next;
	}
}

void bfs()
{
   int i,j,t[9],v,k,l,c=0;
   NODE *aux;
   p=NULL;
   qinsert(initial);
   ins_in2_list(initial);

   while(!qempty())
   {

	   for(i=0;i<9;i++)
	   if(tfront->board[i]==0)
		  break;
		  j=0;
	   while(moves[i][j]>=0&&j<=3)
	  {
		  for(k=0;k<9;k++)
		  t[k]=tfront->board[k];
		  v=t[i];
		  t[i]=t[moves[i][j]];
		  t[moves[i][j]]=v;

		  if(!dublicate(t))
		   {

		   qinsert(t);
			   ins_in2_list(t);
			   for(l=0;l<9;l++)
			   if(final[l]!=t[l])
			  break;
			   if(l==9)
			   {
			  printf("soln found...\n");
			  print=rear;
			  while(print!=NULL)
			  {
				 aux=getnode(print->board);
				 if(p==NULL)
				   p=aux;
				 else
				 {
				   aux->next=p;
				   p=aux;
				 }
				 print=print->parent;
			  }
			  print_soln();
			  exit(1);
			   }
			}
			j++;
	   }
	  tfront=tfront->next;
   }


}

main()
{
	 int i,j;
	 list=NULL;
	 //clrscr();
	 printf("initial state\n");
	  for(i=0;i<9;i++)
	   {
	   printf("%d\t",initial[i]);
	   if((i+1)%3==0)
	   printf("\n");
	   }
	   printf("\n\n");
	printf("final state\n");
	 for(i=0;i<9;i++)
	   {
	   printf("%d\t",final[i]);
	   if((i+1)%3==0)
	   printf("\n");
	   }
	   printf("\n\n");
	
	/* for(i=0;i<9;i++)
		scanf("%d",&initial[i]);*/
  //	 printf("Enter final state\n");
   //	 for(i=0;i<9;i++)
	 //	scanf("%d",&final[i]);

	 bfs();


	 getch();
}
Posted
Comments
CHill60 3-Feb-15 5:19am    
I suggest that you change the title of your post (use the Improve Question link) as it implies that you want code written and actually has nothing to do with the problem.
While you are improving your question you might like to add what the problem actually is, and of which of the two calls to malloc it occurs
Richard MacCutchan 3-Feb-15 6:02am    
What is the error and where does it occur?

What does it mean: "I got this code"?
Did you simply grab it somewhere?
You shouldn't do that. You should try hard to do yourself your own homework.
That said, the "error occurring at the malloc function" is NOT very informative for us. You should detail it in order to get help.
Please read the FAQ.
 
Share this answer
 
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
First, this is C code, not C++.

Second, it's duplicate, not dublicate ;-)

Third, every function, including your main function, must have a return type. Your compiler may just issue a warning, but consider that an error. The main function in C/C++ should always return an int

Fourth, there are two unused variables, i in ins_in2_list(int*) and j in main()

Fifth, your code compiles just fine on my compiler (Visual C++ 10.0). There may be an issue with your compiler or settings. What compiler do you use?

Sixth, it's hard to tell without more info what this is about. Rather than posting the entire code which tells us nothing, you should have posted just the error message that you get, the line of code that causes it, and maybe a few lines of code surrounding that line, to see the context.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900