title: Memory allocation for int array, set in method
body: <p>I am quite new in C and I have a question about understanding one memory allocation issue:</p>
  
  <p>So lets say I allocate memory for an int array:</p>
  
  <pre><code> int* my_array = malloc(sizeof(int)*10);
  </code></pre>
  
  <p>Now I know that a memory block of the given size has been allocated at position '&amp;my_array[0]' (or just my_array).</p>
  
  <p>SO:</p>
  
  <p>If I know call a method to fill my array, I KNOW I should give as a parameter the pointer to my array in order to fill it, so something like:</p>
  
  <pre><code>void fill_array(int* array) {//..do something}
  fill_array(my_array);
  </code></pre>
  
  <p>But I was wondering, what would happen, if the method itself allocates another memory block and I then try to set those equal, so what I mean is:</p>
  
  <pre><code>int* get_filled_array() {
  
      int* result_array = malloc(sizeof(int)*10);
      //fill it somehow...
  
    return result_array;
  }
  </code></pre>
  
  <p>And then I set it like:</p>
  
  <pre><code> int* my_array = malloc(sizeof(int)*10);
   my_array = get_filled_array();
  </code></pre>
  
  <p>Does the memory of my_array gets removed or what is happens? I quess it is wrong this way but was just wondering. 
  I quess if I want to do it like this, I should create an temporary array, get the returned array of (get_filled_array() and then set my_array and the temporary equal, so I can later free the memory of both?</p>