title: Invalid pointer in C - Codewars exercice
body: <p>I'm working on the &quot;Reverse Word&quot; exercice on codewars and the test works but when I try to Attempt I get different error messages:</p>
  <p>Sometimes it will be : Caught unexpected signal: SIGSEGV (11). Invalid memory access.</p>
  <p>Or: free(): invalid size</p>
  <p>Or : double free or corruption (out)</p>
  <p>Or : munmap_chunk(): invalid pointer</p>
  <p>My first question would be what is the meaning of those errors and whats the difference between them ?
  My second question is why does my code work fine when I execute it myself ?</p>
  <p>This is the code I submitted on codewars.</p>
  <p>The instructions are :
  &quot;Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.&quot;</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  char* reverseWords(const char* text)
  {
      char *result;
      result = malloc (sizeof text);
      strcpy(result, text);
      int i, j, h, length, temp, index, count, tempo;
      count = 0;
      length = strlen(result);
  
      for (i=0; i &lt; length ; i++)
      {
          if (result[i] == ' ' || i == length - 1)
          {
              count++;
              if (count == 1)
              {
                  index = -1;
                  tempo = i;
              }
              else if (count &gt; 1)
              {
                  index = tempo;
                  tempo = i;
              }
  
              h = i;
  
              if (count &gt;= 1 &amp;&amp; i == length - 1 &amp;&amp; result[i] != ' ')
              {
                  h = length;
              }
              if (i != 0)
              {
                  for (j=(index + 1); j &lt; ((index +i)/2) + 1 ; j++)
                  {
                      temp = result[j];
                      result[j] = result[h -1];
                      result[h -1] = temp;
                      h--;
                  }
              }
          }
      }
      return (result);
  }
  </code></pre>
  <p>And this is my main function:</p>
  <pre><code>int main(void)
  {
      char * p; 
      const char text[] = &quot; double  spaces&quot;;
      p = reverseWords(text);
      printf(&quot;%s\n&quot;, p);
      return EXIT_SUCCESS;
  }
  </code></pre>
  <p>Also here is the test code of CW:</p>
  <pre><code>#include &lt;criterion/criterion.h&gt;
  #include &lt;stdlib.h&gt;
  
  char* reverseWords(const char* text);
  
  Test(Sample_Tests, should_pass_all_the_tests_provided) {
    {
      const char* test_str = &quot;apple&quot;;
      const char* expected = &quot;elppa&quot;;
      char* submitted = reverseWords(test_str);
      cr_assert_str_eq(expected, submitted,
                      &quot;Expected:\n\&quot;%s\&quot;\n\nSubmitted:\n\&quot;%s\&quot;\n\n&quot;, expected, submitted);
      free(submitted); submitted = NULL;
    }
    {
      const char* test_str = &quot;a b c d&quot;;
      const char* expected = &quot;a b c d&quot;;
      char* submitted = reverseWords(test_str);
      cr_assert_str_eq(expected, submitted,
                      &quot;Expected:\n\&quot;%s\&quot;\n\nSubmitted:\n\&quot;%s\&quot;\n\n&quot;, expected, submitted);
      free(submitted); submitted = NULL;
    }
    {
      const char* test_str = &quot;double  spaced  words&quot;;
      const char* expected = &quot;elbuod  decaps  sdrow&quot;;
      char* submitted = reverseWords(test_str);
      cr_assert_str_eq(expected, submitted,
                      &quot;Expected:\n\&quot;%s\&quot;\n\nSubmitted:\n\&quot;%s\&quot;\n\n&quot;, expected, submitted);
      free(submitted); submitted = NULL;
    }
    {
      const char* test_str = &quot;The quick brown fox jumps over the lazy dog.&quot;;
      const char* expected = &quot;ehT kciuq nworb xof spmuj revo eht yzal .god&quot;;
      char* submitted = reverseWords(test_str);
      cr_assert_str_eq(expected, submitted,
                      &quot;Expected:\n\&quot;%s\&quot;\n\nSubmitted:\n\&quot;%s\&quot;\n\n&quot;, expected, submitted);
      free(submitted); submitted = NULL;
    }
  }
  </code></pre>
  <p>I'm having a bit of trouble understanding pointers and dynamic memory allocation so would be nice if somebody could enlighten me ! :)</p>
  <p>Cheers</p>