title: C language error munmap_chunk(): invalid pointer Aborted (core dumped)
body: <p>I met with this error in my project. The related codes are as follows:
  I allocate memory for the pointer "values" as follows:</p>
  
  <pre><code>data-&gt;TSSet[0].values = (float *)malloc(sizeof(float)*10000);
  </code></pre>
  
  <p>Then after using it, I release the values as follows:</p>
  
  <pre><code>free(data-&gt;TSSet[0].values);
  </code></pre>
  
  <p>Then I met with the error  munmap_chunk(): invalid pointer Aborted (core dumped). The pointer "values" is valid because when I try to print out the contents in "values", like follows:</p>
  
  <pre><code>for(i=0;i&lt;TSSet[0].length;i++)
      printf("%f\n",TSSet[0].values[i]);
  </code></pre>
  
  <p>I get the correct output.
  Why does the error happens?
  The full version of code is as follows:(I add comments on the key part, if you don't want to read the long code, just jump to the comments)
  In the main function, I just use ReadCSV(file, &amp;data) to read in data, then immediately call ResetData(&amp;data).
  Thank you all for helping me!!!!</p>
  
  <pre><code>void ReadCSV(char *file_path, DATA *Data){
      Data-&gt;tsname = (char*)malloc(sizeof(char)*MAXNAME);
      Data-&gt;TSSet = (TSItem*)malloc(sizeof(TSItem)*MAXNITEM);
      FILE *fp = fopen(file_path, "r");
      if(fp == NULL)
          return ;
      char line[MAXSTRL];
      char *save_ptr,*tk;
      int id = 0, num_class = 0, i,label;
      Data-&gt;ls_class = (int *)malloc(sizeof(int)*MAXC);
      Data-&gt;num_item = 0;
      Data-&gt;length = 0;
      while(fgets(line, sizeof(line), fp)) {
          tk = strtok_r(line,",", &amp;save_ptr);
          if (tk == NULL)
              return ;
          label = atoi(trim(tk));
          Data-&gt;TSSet[id].label = label;
          Data-&gt;TSSet[id].num_nbr = 0;
          if(IsExist(label,Data-&gt;ls_class, num_class) == 0){
              Data-&gt;ls_class[num_class] = label;
              num_class++;
          }
          // malloc the memory for values
          Data-&gt;TSSet[id].values = (float *)malloc(sizeof(float)*MAXL);
          Data-&gt;TSSet[id].knn = (Neighbor*)malloc(sizeof(Neighbor)*MAXK);
          i=0;
          // read in content of values from files
          while(tk!=NULL){
              tk = strtok_r(NULL,",", &amp;save_ptr);
              Data-&gt;TSSet[id].values[i] = strtof(trim(tk),NULL);
              i++;
              if(strlen(save_ptr) == 0)
                  break;
          }
          Data-&gt;TSSet[id].predlbl = -100;
          Data-&gt;TSSet[id].conf = 0.0;
          Data-&gt;TSSet[id].entropy = 0.0;
          Data-&gt;TSSet[id].length = i;
          id++;
      }
      if(fp == NULL)
          printf("NULL!!!!!!!!!!!!!!!!!!!!!\n");
      fclose(fp);
      Data-&gt;num_item = id;
      Data-&gt;length = i;
      Data-&gt;num_class = num_class;
  }
  
  void ResetData(DATA *data){
      int i;
      free(data-&gt;tsname);
      for(i=0;i&lt;data-&gt;num_item;i++){
      // error here
          free(data-&gt;TSSet[i].values);
          free(data-&gt;TSSet[i].knn);
      }
      free(data-&gt;TSSet);
      data-&gt;num_item = 0;
      data-&gt;num_class = 0;
      free(data-&gt;ls_class);
      data-&gt;length = 0;
      return;
  }
  </code></pre>