title: Alternately glibc detected free(): invalid next size (fast)
body: <p>Please help, I'm so new in C that I don't understand the problem
  I have 3 files
  main.c, set.c, set.h
  in set.h</p>
  
  <pre><code>typedef struct Set{unsigned char array[16];
  }Set;
  </code></pre>
  
  <p>in main.c
  main method</p>
  
  <pre><code>int main
  {
      int i=0;
      char *input="read_set A,0,1,2,3,4";
      char *output[LEN_ACTIONS];
      char *d=" ";
  
      read_input(input, output, d,0);
      char *methodName = output[0];
       printf("method name:%s\n", methodName);
  
      invoke_method(methodName, output);
  
      for(i=0; i &lt; LEN_ACTIONS; i++)
          free(output[i]);
  
      return 0;
  }
  </code></pre>
  
  <p>in set.c</p>
  
  <pre><code>void read_input(char *input,char **output, char *delimiter, int onlyNumbers)
  {    
      int index =0;
      char *str =(char*)malloc(strlen(input));
      strcpy(str, input);
      char *tok=NULL;
       tok = strtok(str,delimiter);
  
      while(tok!=NULL)
      {
          char *dup = (char*)malloc(strlen(tok)+1);
          if(dup)
          {
              if(onlyNumbers &gt;0)
              {
                  if(is_number(tok))
                   {
                      output[index] = strcpy(dup, tok);
                      index++;
                  }
              }    
              else
              {
                  output[index] = strcpy(dup, tok);
                   index++;
              }    
          }
  
          tok = strtok(NULL, delimiter);
      }
      free(str);
  }
  </code></pre>
  
  <p>in main.c</p>
  
  <pre><code>void invoke_method(char *methodName, char **output)
  {
      int i=0;
      char *d=",";
      char *groups[5];
      read_input(output[1], groups, d, 0);
  
      if(strcmp(methodName,"read_set") ==0)
       {
          printf("group: %s\n", groups[0]);
  
          d=",";
          char *numbers[MAX_IN_SET]={NULL};
          read_input(output[1], numbers, d, 1);
  
          if(strcmp(groups[0], "A")==0)
           {
              printf("init A set\n");
              initialize_set(&amp;A); //initialize the array with 0
              printf("input to set\n");
              read_set(&amp;A, numbers);
          }
          if(strcmp(groups[0], "B")==0)
           {
              initialize_set(&amp;B);
              read_set(&amp;B, numbers);
          }
          if(strcmp(groups[0], "C")==0)
          {
              initialize_set(&amp;C);
              read_set(&amp;C, numbers);
           }
          if(strcmp(groups[0], "D")==0)
          {
              initialize_set(&amp;D);
              read_set(&amp;D, numbers);
          }
          if(strcmp(groups[0], "E")==0)
           {
              initialize_set(&amp;E);
              read_set(&amp;E, numbers);
          }
      }
  
  
      for(i=0; i&lt;5; i++)
          free(groups[i]);
   }
  </code></pre>
  
  <p>in set.c</p>
  
  <pre><code>void read_set(struct Set *set, char **numbers)
  {
      int i=0;
      for(i=0; i&lt;MAX_IN_SET; i++)
      {
  
          if(numbers[i] != NULL &amp;&amp; numbers[i] != '\0')
          {
               int number = atoi(numbers[i]);
              int charIndex = number/8;
              int bitIndex = number %8;
              int bitPlace = 1&lt;&lt;(7-bitIndex);
              unsigned char c = (unsigned char)bitPlace;
               int numberExists = c &amp; set-&gt;array[charIndex];
              if(!numberExists)
              {
                  set-&gt;array[charIndex] = c|set-&gt;array[charIndex];
                  /*printf("number %d has been added\n", number);*/
               }
          }
      }
  }
  </code></pre>
  
  <p>in terminal after I run this I get an error</p>
  
  <p><strong>* glibc detected*</strong> ./mainprog: free(): invalid next size (fast)</p>
  
  <p>Thank you very much</p>