title: Segmentation fault on assigning to char*
body: <p>So I'm writing this code here and I simply want a function to return a character array and assign it to a variable in main function. Here is the function returning char*  </p>
  
  <pre><code>char* getString(pid_t pid, long address, long length) 
  { 
      char* str =  (char *)malloc((length+1) * sizeof(char));
      int i =0;
  
      while (i &lt; length)
      {
          char t = ptrace(PTRACE_PEEKDATA,pid,address+i,0); 
          str[i] = t; 
          i++;
      }
      //  str[length] = '\0';
      printf(" Exiting getString %s \n ", str);
      return str;
  }
  </code></pre>
  
  <p>And here is what I do in main </p>
  
  <pre><code>int main { 
      ... 
      ...  
      char* st = getString(pid, arguments[1], arguments[2]); // causes seg fault 
      // printf("getstring %s\n", getString(pid, arguments[1], arguments[2])); works fine
      // and produces correct output 
      return 1;
  }
  </code></pre>
  
  <p>I haven't done much in C lately so maybe I'm missing something, but why does printf works and assigning fails? Can someone tell me what am I doing wrong here? </p>