title: Memory issue in C where data is being overwritten
body: <p>I've the following program:</p>
  
  <pre><code>// required include statements...
  
  static char ***out;
  
  char* get_parameter(char* key)
  {
      char *querystr = /*getenv("QUERY_STRING")*/ "abcdefg=abcdefghi";
      if (querystr == NULL)
          return (void*)0;
  
      char s[strlen(querystr)] ;
      strcpy(s, querystr);
      const char delim = '&amp;';
      const char delim2 = '=';
      static size_t size = 0;
      if (out == 0)
      {
          out = (char*) malloc(sizeof(char*));
          size = split(s, &amp;delim, out);
      }
  
      int i=0;
      for (; i&lt;size; i++)
      {
          if ((*out)[i] != NULL)
          {
              char ***iout = NULL;
              iout = (char*) malloc(sizeof(char*));
              int isize = split((*out)[i], &amp;delim2, iout);
  
              if (isize &gt; 1 &amp;&amp; ((*iout)[1]) != NULL &amp;&amp; strcmp(key, (*iout)[0]) == 0)
              {
                  size_t _size = strlen((*iout)[1]);
                  char* value = (char*) malloc(_size*sizeof(char));
                  strcpy(value, (*iout)[1]);
                  free(iout);
                  return value;
              }
          }
      }
      return (void*) 0;
  }
  
  static size_t count(const char *str, char ch)
  {
      if (str == NULL) return 0;
      size_t count = 1;
      while (*str)
          if (*str++ == ch) count++;
      return count;
  }
  
  size_t split(const char *const str, const char* delim, char ***out)
  {
      size_t size = count(str, *delim);
      *out = calloc(size, sizeof(char));
      char* token = NULL;
      char* tmp = (char*) str;
      int i=0;
      while ((token = strtok(tmp, delim)) != NULL)
      {
          tmp = NULL;
          (*out)[i] = (char*) malloc(sizeof strlen(token));
          strcpy((*out)[i++], token);
      }
      return size;
  }
  
  main()
  {
      char* val = get_parameter("abcdefg");
      printf("%s\n", val);  // it should prints `abcdefghi`, but it prints `abcd?`
      free(val);
  }
  </code></pre>
  
  <p>as appears in the main method, the function get_parameter should prints <code>abcdefghi</code>, but it prints <code>abcd?</code> where <code>?</code> is a controls character with value of 17.</p>
  
  <p>Why the reset of string is not printed? I think I mis-used the <code>malloc</code> to allocate appropriate space.</p>
  
  <p>Also, is there any tool that I can use to know the internal representation of memory for my pointers?</p>