title: C: free(): invalid pointer Aborted (core dumped)
body: <p>I have a program in which I try to read two lines of 40 characters. When something goes wrong or the program ends, I free array.</p>
  <p>I'm getting an error message free(): invalid pointer Aborted (core dumped)</p>
  <p>I think the problem is in the last cycle, but I can't solve it.</p>
  <pre><code>char *str = NULL;
  size_t  capacity = 0;
  char **maska;
  int rMaska=2, cMaska = 40;
  maska =(char**)malloc (rMaska*sizeof(*maska));
      
      
      for (int a = 0; a &lt; rMaska; a++)
          maska[a]=(char *) malloc (cMaska*sizeof(**maska));
  
      i=0;
      printf(&quot;Maska:\n&quot;);
      while ( getline (&amp;str, &amp;capacity, stdin) != -1)
      {
          if (i==rMaska)
      {
          printf(&quot;incorrect entry.\n&quot;);
          for (int i = 0; i &lt; rMaska; i++)
              free (maska[i]);
          free(maska);
  
          return 0;
      }   
          if( strlen(str) != (unsigned int) cMaska )
              {
                  printf(&quot;incorrect entry.\n&quot;);
                  
                  for (int i = 0; i &lt; rMaska; i++)
                      free (maska[i]);
                  free(maska);
                      
                  return 0;
              }
              
          for (int i = 0; i &lt; cMaska-1; i++)
              {
                  if ( (str[i] != '#') &amp;&amp; (str[i] != '.') )
                  {
                      printf(&quot;incorrect entry.\n&quot;);
                      for (int a = 0; a &lt; rMaska; a++)
                          free (maska[a]);
                      free(maska);
                      
                      return 0;
                  }
              }
          
          strcpy(maska[i], str);
          i++;
      }
      
      free(str);
      
      for (int i = 0; i &lt; rMaska; i++)
          free (maska[i]);
      free(maska);
      
  </code></pre>