title: Error allocating memory for temp in queue push in c invalid write size of 8 from valgrind
body: <p>This is my push function</p>
  
  <pre><code>void push(struct Map *map, struct Location location){
      struct Node *temp = map-&gt;front;
      temp-&gt;loc = location; //this line causes the error
      temp-&gt;next = NULL;
  
      if(map-&gt;rear == NULL) { // if queue empty
          map-&gt;front = temp; // First NODE 
          map-&gt;rear = map-&gt;front;
      }else{// if there is stuff in the queue
          map-&gt;rear-&gt;next = temp; 
          map-&gt;rear = temp;
          // Insert End 
      }
      free(temp);
  }
  </code></pre>
  
  <p>and I am getting this error</p>
  
  <pre><code>==2301== Invalid write of size 8
  ==2301==    at 0x401148: push (3146.c:239)
  ==2301==    by 0x400DE7: findEntrance (3146.c:164)
  ==2301==    by 0x400820: main (3146.c:55)
  ==2301==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
  ==2301== 
  </code></pre>
  
  <p>I am doing a breadth first search on a grid of characters arranged to be a maze. I have been having trouble getting no memory leaks so I have finally found that using malloc helps, but I don't understand why I cannot do this data assignment without a segfault</p>
  
  <p>The alternative is to not free it and have memory leaks, which I don't think is an acceptable solution. Where is my logic wrong?</p>
  
  <p>Below this is the whole program </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct Location;
  struct Map;
  struct Node;
  
  //function names seem to self describe
  void pathFinder(struct Map*);
  void findEntrance(struct Map*);
  void readInMap(struct Map*);
  void printMap(struct Map*);//to screen
  /* Q funcs */
  void pop(struct Map*);
  void push(struct Map*, struct Location);
  
  
  struct Location {// simple location type 
      int x;
      int y;
  };
  struct Node {//standard linked list node
      struct Location loc;
      struct Node *next;
  };
  struct Map { //variable size encompassing array, and dimension variables
      char arr[100][100];
      int xLength;//dimensions
      int yLength;
      int complete;
      struct Node *rear;//Q pointers
      struct Node *front;
      struct Node *currLoc; //temp for BFS
      struct Location entrance;
  };
  
  
  int main(){
      struct Map map;//the one map to rule them all
      map.xLength = 0;//map dimensions
      map.yLength = 0;
      map.front = NULL; // queue pointers init
      map.rear = NULL;
      map.currLoc = NULL;
      map.entrance.x = 0;
      map.entrance.y = 0;
      map.complete = 0;
  
      readInMap(&amp;map);
      findEntrance(&amp;map);//start off by finding where to start
      pathFinder(&amp;map); // problem solver method 
      printMap(&amp;map);
  
      free(map.front);
      free(map.rear);
      free(map.currLoc);
      return 0;
  
  }
  
  void pathFinder(struct Map *map){
      int x;//should be the entrance already pushed from findEntrance
      int y;
      int done = 0;
      struct Location temp;
      temp.x = 0;// temp for pushing locations that are adjacent 0's
      temp.y = 0;
  
      while(map-&gt;front != NULL){
          map-&gt;currLoc = map-&gt;front;//currLoc is just a copy so that we can pop the map location of currLoc from Q
          pop(map);// pop so it cant be used twice in this loop
          x = map-&gt;currLoc-&gt;loc.x;
          y = map-&gt;currLoc-&gt;loc.y;
          temp.x = 0;
          temp.y = 0;
  
          if(map-&gt;arr[y][x] == '0'){//protection of 1 values from replacement
              map-&gt;arr[y][x] = 'X';//replace the array position loc w x
          }
  
          if(map-&gt;arr[y][x] == '0'){//protection of 1 values from replacement
              map-&gt;arr[y][x] = 'X';//replace the array position loc w x
              printf("test\n");
          }
              /* find adjacent 0's */
          if((y &lt; map-&gt;xLength)&amp;&amp;(map-&gt;arr[y+1][x] == '0')){
              temp.x = x;
              temp.y = y+1;
              push(map, temp);
          }
          if((x &lt; map-&gt;xLength)&amp;&amp;(map-&gt;arr[y][x+1] == '0')){
              temp.x = x+1;
              temp.y = y;
              push(map, temp);
          }
          if((y &gt; 0)&amp;&amp;(map-&gt;arr[y-1][x] == '0')){
              temp.x = x;
              temp.y = y-1;
              push(map, temp);
          }
          if((x &gt; 0)&amp;&amp;(map-&gt;arr[y][x-1] == '0')){
              temp.x = x-1;
              temp.y = y;
              push(map, temp);
          }
          if((x == 0)||(x == map-&gt;xLength)||(y == 0)||(y == map-&gt;yLength)){  //then its not on the edge
              if((x != map-&gt;entrance.x)&amp;&amp;(y != map-&gt;entrance.y)){
                 map-&gt;front = NULL;
                 map-&gt;complete++;
              }
          }
      } 
  }
  
  void findEntrance(struct Map *map){
      struct Location entrance;
      int done = 0;
      int y=0;//index of current row depth
      int x=0;//index of current column depth
      if (done == 0){
          for (x=0;x&lt;=map-&gt;xLength;x++){ // top row
              if (map-&gt;arr[y][x] == '0') {
                  entrance.x = x;
                  entrance.y = y;
                  done = 1;
              }
          }
      }
      if (done == 0){
          for(y=0;y&lt;=map-&gt;yLength;y++){//down the right side
              if (map-&gt;arr[y][map-&gt;xLength] == '0') {
                  entrance.x = x;
                  entrance.y = y;
                  done = 1;
              }
          }
      }
      if (done == 0){
          for(x;x&gt;=0;x--){//bottom row RtoL
              if (map-&gt;arr[map-&gt;yLength][x] == '0') {
                  entrance.x = x;
                  entrance.y = y;
                  done = 1;
              }
          }
      }
      if (done == 0){
          for(y;y&gt;=0;y--){//up the left side
              if (map-&gt;arr[y][0] == '0') {
                  entrance.x = x;
                  entrance.y = y;
                  done = 1;
              }
          }
      }
  
      map-&gt;entrance = entrance;
      push(map, map-&gt;entrance);
  }
  
  void readInMap(struct Map *map){
      FILE *fptr;
      char c;
      char file_name[20];
      int i,j;
      int rows;
      int cols;
      int x,y;
      printf("Size\n");
      printf("Rows:");
      scanf("%i", &amp;rows);
      printf("Columns:");
      scanf("%i", &amp;cols);
  
      map-&gt;xLength = cols-1; //[y][xLength]
      map-&gt;yLength = rows-1; //[yLength][x]
      for (x=0;x&lt;100;x++){
          for (y=0;y&lt;100;y++){
              map-&gt;arr[x][y] = '1';//whole array is 1
          }
      }
  
      printf("Type in the name of the file containing the Field\n");
      scanf("%s",file_name);
      fptr=fopen(file_name, "r");
      for (i = 0; i &lt;= map-&gt;xLength; i++){
          for (j = 0; j &lt;= map-&gt;yLength; j++){
              c=fgetc(fptr); //handles new line character and spaces
              while ( !((c == '1') || (c =='0')) ) {
                  c=fgetc(fptr);
              }
              map-&gt;arr[i][j] = c;
          }
      }
      printf("\n");
      fclose(fptr);
  }
  
  void printMap(struct Map *map){
      int y;//index of current row depth
      int x;//index of current column depth
      for (x=0;x&lt;=map-&gt;xLength;x++){
          for (y=0;y&lt;=map-&gt;yLength;y++){//current pos (x,y)
              printf("%c", map-&gt;arr[x][y]);
          }
          printf("\n");
      }
      if(map-&gt;complete != 0){
          printf("\n An exit to the maze was found that is not the entrance\n");
      }else{
          printf("\n No exit was found  \n");
      }
  }
  
  
  void pop(struct Map *map){
      if (map-&gt;front != NULL){//if not empty
          if (map-&gt;front == map-&gt;rear){
              map-&gt;rear = NULL;//if the line length is 1, empty it
              map-&gt;front = NULL;
          }else{
              map-&gt;front = map-&gt;front-&gt;next;//next in line
          }
      }else{
          map-&gt;rear = NULL;//empty out the queue
          map-&gt;front = NULL;
      }
  }
  
  void push(struct Map *map, struct Location location){
      struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
      temp-&gt;loc = location;
      temp-&gt;next = NULL;
  
      if(map-&gt;rear == NULL) { // if queue empty
          map-&gt;front = temp; // First NODE 
          map-&gt;rear = map-&gt;front;
      }else{// if there is stuff in the queue
          map-&gt;rear-&gt;next = temp; 
          map-&gt;rear = temp;
          // Insert End 
      }
      free(temp);
  }
  </code></pre>