title: Getting the return address of a function in C
body: <p>I have some C code that essentially looks something like</p>
  
  <pre><code>double* my_function(double* input) {
      double* output = (double*) malloc(...);
      // Do some stuff to output based on input
      return output;
  }
  </code></pre>
  
  <p>Assuming that I have already built an array called <em>my_var_in</em>, my main code has something like</p>
  
  <pre><code>double* my_var_out = my_function(my_var_in);
  </code></pre>
  
  <p>This creates <em>new</em> memory, which <em>my_var_out</em> points to; most of the time that's exactly what I want to happen. However, sometimes I just want to <em>update</em> the already existing memory. If I write</p>
  
  <pre><code>double* my_var = ... ; // allocate and fill my_var as desired
  my_var = my_function(my_var);
  </code></pre>
  
  <p>Then I lose the pointer to the previously existing memory since <em>my_var</em> now points to the newly allocated memory. I've played around with several different ideas for how to get around this.</p>
  
  <p>1) I know I can get this to work by changing the function style to something like</p>
  
  <pre><code>void my_function(double* destination, double* source) {
      // Check if destination == source and update based on this
  </code></pre>
  
  <p>I've previously developed with this style of passing the destination pointer as an input argument and updating it there; it works great functionally, but I like the style of outputting pointers better and want to keep developing with it.</p>
  
  <p>2) Based on <a href="https://stackoverflow.com/questions/8094024/how-to-know-the-return-address-of-a-function">here</a> and <a href="https://stackoverflow.com/questions/23818282/how-to-find-the-return-address-of-a-function-in-c/">here</a>, it seems that the <strong>__builtin_return_address</strong> function could be of some use. I think I can use this function to compare the input address with the output address and allocate/update based on that. After reading the documentation <a href="https://gcc.gnu.org/onlinedocs/gcc-4.6.2/gcc/Return-Address.html" rel="nofollow noreferrer">here</a>, I wrote the following test</p>
  
  <pre><code>int* just_testing(int* in) {
      void* ptr = __builtin_return_address(0); // the return address of just_testing
      printf("Input address: %p\n", in);
      printf("From function: %p\n", ptr);
  
      return in;
  }
  
  int main(int argc, char** argv) {
  
      int* out = (int*) malloc(sizeof(int));
      out[0] = 2;
      printf("From main before call: %p\n", out);
      out = just_testing(out);
      printf("From main after call: %p\n", out);
  
      return 0;
  }
  </code></pre>
  
  <p>This gives me the output</p>
  
  <pre><code>From main before call: 0x19b2010
  Input address: 0x19b2010
  From function: 0x4011f1
  From main after call: 0x19b2010
  </code></pre>
  
  <p>Unfortunately, the results from <strong>__builtin_return_address</strong> don't match the address of the variable that receives the return from the function, which I did not expect.</p>
  
  <p>Why aren't the addresses the same? I also welcome any constructive suggestions on a different approach besides the two I've listed here.</p>