title: Why same variable got different virtual addresses at different runs?
body: <p>I need to know the virtual address of the variables (the heap variable, in particular) in my project. The pointer value of the variable is actually its virtual address. My understanding is that the Virtual Address of the same variable should the same at different runs. I wrote the following simple code to prove my thoughts, but it turns out to be wrong,</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int main(int argc, char **argv)
  {
      int node=0;
      char* buffer;
      int i;
      printf("Hello World from Node %d the vm of i is %p and %ld \n",node, &amp;i,&amp;i);
      buffer = (char*)malloc(sizeof(char)*1024*1024*300); 
      for(i = 0; i &lt; 1024*1024*300; i++){
      buffer[i] = (char)1;
  }   
  printf("I  am in %d and the vm of buffer is %p:%ld \n",node, buffer,buffer);
  return 0;
  }
  </code></pre>
  
  <p>But I got different pointer values for different runs as follows,</p>
  
  <pre><code>-bash-4.0$ gcc singletest.c
  -bash-4.0$ ./a.out 
  Hello World from Node 0 the vm of i is 0x7fffb87a8e00 and 140736288427520 
  I  am in 0 and the vm of buffer is 0x7fb05dfcf010:140395467829264 
  -bash-4.0$ ./a.out 
  Hello World from Node 0 the vm of i is 0x7fffec2856f0 and 140737155454704 
  I  am in 0 and the vm of buffer is 0x7f5888c54010:140018228477968 
  -bash-4.0$ ./a.out 
  Hello World from Node 0 the vm of i is 0x7fff1f44a780 and 140733717981056 
  I  am in 0 and the vm of buffer is 0x7fbea3b91010:140456767328272 
  </code></pre>
  
  <p>I also wrote a simple MPI code that each process simply generates exactly the same variable. And the same variable for different processes have the different pointer values, which is not as I expected.</p>
  
  <p>Can any one explain this to me ?
  Thanks,</p>