title: segmentation fault in c linux but not in windows
body: <p>I am trying to write a code to split given Path by the character ":" and there is my code :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void parser()
  {
      char ** res  = NULL;
      char *  p    = strtok (getenv("PATH"), ":");
      int n_spaces = 0, i;
  
      /* split string and append tokens to 'res' */
  
       while (p)
       {
           res = realloc (res, sizeof (char*) * ++n_spaces);
  
           if (res == NULL)
               exit (-1); /* memory allocation failed */
  
           res[n_spaces-1] = p;
  
           p = strtok (NULL, ":");
       }
  
       /* realloc one extra element for the last NULL */
  
        res = realloc (res, sizeof (char*) * (n_spaces+1));
        res[n_spaces] = 0;
  
      /* print the result */
  
        for (i = 0; i &lt; (n_spaces+1); ++i)
            printf ("res[%d] = %s\n", i, res[i]);
  
      /* free the memory allocated */
  
       free (res);
  }
  
  int main(int argc , char* argv[])
  {
      parser();
      return 0;
  }
  </code></pre>
  
  <p>this code gives me segmentation fault in linux but when a tried to run it on windows , it worked fine !!  </p>