title: pthread argument address is different than before
body: <p>I create 2 threads, and main() will pass argc and argv to thread, so I use <code>struct arg</code> that contains <code>argc</code> and <code>argv</code>. </p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  struct arg {
     int argc;
     char id[9];
     char **argv;
  };
  
  int test1(struct arg *a) {
      int argc;
      char **argv;
      argc = a-&gt;argc;
      argv = a-&gt;argv;
  
      if (argc &gt; 3) {
          printf("arg 1 is %s, arg 2 is %s arg 3 is %s\n\n", *(argv+1), *(argv+2),a-&gt;id);
      }
      else {
          exit(0);
      }
  
  
      return 0;
  }
  int main(int argc, const char *argv[]) {
      int i;
      void *ret;
      struct arg *a = (struct arg *) malloc(sizeof(struct arg *) * atoi(argv[3]));
      pthread_t *thread = (pthread_t *) malloc(sizeof(*thread) * atoi(argv[3]));
  
      for(i = 0; i &lt; atoi(argv[3]); ++i) {
          a[i].argc = argc;
          a[i].argv  = argv;
          sprintf(a[i].id,"%d",i);
          pthread_create( &amp;thread[i], NULL , test1 ,(struct arg *)&amp;(a[i]));
      }
  
      for(i = 0; i &lt; atoi(argv[3]); ++i) {
           pthread_join( thread[i], &amp;ret);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I exec my program:</p>
  
  <pre><code>./test 1 2 2
  </code></pre>
  
  <p>The output looks like:</p>
  
  <pre><code>arg 1 is 1, arg 2 is 2 arg 3 is 0
  
  arg 1 is , arg 2 is  arg 3 is 1
  </code></pre>
  
  <p>In thread 1 the argv is correct, but in thread 2 the <code>argv</code> address is different than before.</p>
  
  <p>I use gdb to print a->argv address </p>
  
  <ul>
  <li>in main argv address is 0x7fffffffdee8</li>
  <li>in thread 1 argv address is 0x7fffffffdee8</li>
  <li>in thread 2 argv address is 0x7ffff6fef700</li>
  </ul>
  
  <p>[Switching to Thread 0x7ffff6fef700 (LWP 19472)]</p>
  
  <p>argv address is the same as thread address</p>
  
  <p>I check arg[1]->argv before pthread_create</p>
  
  <p>the address is 0x7fffffffdee8.</p>
  
  <p>Will <code>pthread_create</code> change the address?</p>
  
  <p>Is there something I'm missing?</p>