title: Memory error while implementing BFS(Breadth First Search)
body: <p>I am trying to implement BFS of graph.
  These are my header files-</p>
  
  <p><strong>nodes.h</strong></p>
  
  <pre><code>typedef struct Node
  {
      int data;
      struct Node* next;
  }Node;
  
  typedef struct Queue
  {
      Node * front;
      Node * rear;
  }Queue;
  </code></pre>
  
  <p><strong>control.h</strong></p>
  
  <pre><code>#include"nodes.h"
  Queue * CreateQueue();
  int isEmptyQueue(Queue * Q);
  void EnQueue(Queue * Q,int data);
  int DeQueue(Queue * Q);
  void DeleteQueue(Queue * Q);
  </code></pre>
  
  <p>This is my code which implements the queue to be used for BFS(breadth first search).This works fine when I run it without my BFS code.But with BFS code it gives memory allocation error(I have mentioned my error at the last of my question)in the function <code>CreateQueue()</code> when I allocate memory for the <code>Queue</code>.</p>
  
  <p><strong>queue.c</strong></p>
  
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include"control.h"
  
  
  Queue * CreateQueue()
  {
      Queue * Q;
      Node * temp;
      Q=(Queue *)malloc(sizeof(Queue));printf("Hello\n");
      if(!Q)
      return NULL;
  
      temp=malloc(sizeof(Node));
      Q-&gt;front=Q-&gt;rear=NULL;
      return Q;
  }
  
  int isEmptyQueue(Queue * Q)
  {
      return(Q-&gt;front==NULL);
  }
  
  void EnQueue(Queue * Q,int data)
  {
      Node * newNode;
      newNode=malloc(sizeof(Node));
      newNode-&gt;data=data;
      newNode-&gt;next=NULL;
  
  
      if(Q-&gt;rear)
          Q-&gt;rear-&gt;next=newNode;
      if(Q-&gt;front==NULL)
          Q-&gt;front=Q-&gt;rear=newNode;
  }
  
  int DeQueue(Queue * Q)
  {
      int data;
      Node * temp;
      if(isEmptyQueue(Q))
       return 0;
      else
          {
              temp=Q-&gt;front;
              data=Q-&gt;front-&gt;data;    
              Q-&gt;front=temp-&gt;next;
              //free(temp);
          }
          return data;
  }
  </code></pre>
  
  <p>This is my BFS code which makes use of <code>queue.c</code></p>
  
  <p><strong>BFS.c</strong></p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include"control.h"
  
  typedef struct Graph
  {
    int V;
    int E;
    int * visited;
    int * vis;
    int ** Adj;
  }Graph;
  
  
  Graph * adjMatrix()
  {
      int i,j,v2,v1;
  
      Graph * G = (Graph *)malloc(sizeof(Graph));
  
      FILE * input;
  
      input=fopen("input.txt","r");
      fscanf(input,"%d",&amp;G-&gt;V);
      fscanf(input,"%d",&amp;G-&gt;E);
      G-&gt;Adj = (int **)malloc(G-&gt;V * sizeof(int *));
      G-&gt;visited = (int *)malloc(G-&gt;V * sizeof(int ));
      G-&gt;vis = (int *)malloc(G-&gt;V * sizeof(int ));
      for (i=1; i&lt;=G-&gt;V; i++)
           G-&gt;Adj[i] = (int *)malloc(G-&gt;V * sizeof(int));
      for(i=1;i&lt;=G-&gt;V;i++)
      {
        for(j=1;j&lt;=G-&gt;V;j++)
          {   
              G-&gt;Adj[i][j]=0;
  
          }
      }
      fscanf(input,"%d %d",&amp;v1,&amp;v2);
      while(!feof(input))
        {
  
  
              G-&gt;Adj[v1][v2]=1;
              G-&gt;Adj[v2][v1]=1;
              fscanf(input,"%d %d",&amp;v1,&amp;v2);
        }
  
      return G;
  }
  
  
  void BFS(Graph * G,int u)
  {   
      int i;
      Queue * Q=CreateQueue();
      EnQueue(Q,u);
      while(!isEmptyQueue(Q))
          {
              u=DeQueue(Q);
              printf("%d\n",u);
              //Process v
              G-&gt;visited[u]=1;
              for(i=1;i&lt;=G-&gt;V;i++)
                  {
                      if(!G-&gt;visited[i] &amp;&amp; G-&gt;Adj[u][i]==1)
                          EnQueue(Q,i);
                  }
          }
  }
  
  int main()
  {   
      Graph * G=adjMatrix();
      int i,j,k,count=0;
  
      //Display the graph
      for(i=1;i&lt;=G-&gt;V;i++)
       {
          for(j=1;j&lt;=G-&gt;V;j++)
            printf("%d  ",G-&gt;Adj[i][j]);
          printf("\n");
       }
  
       BFS(G,3);
  }
  </code></pre>
  
  <p>I am using matrix representaion and am taking input from the file.The code is correct the  problem is when I call <code>CreateQueue</code>in BFS.I get runtime error as this-</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>exe: malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &amp;((av)-&gt;bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) &amp;&amp; old_size == 0) || ((unsigned long) (old_size) &gt;= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) &amp; ~((2 * (sizeof(size_t))) - 1))) &amp;&amp; ((old_top)-&gt;size &amp; 0x1) &amp;&amp; ((unsigned long)old_end &amp; pagemask) == 0)' failed.
  Aborted (core dumped)
  </code></pre>
  
  <p>Any help would be highly appreciated.Thanks.</p>