title: Redirecting pointers in C memory leak
body: <p>When I run valgrind on this function, it says I've definitely lost 4 bytes. I know it's because I'm redirecting the pointer x in x=y, thus losing access to the initial memory allocated in the first line. How do I fix this? What's the correct principle here? I'm just learning C, so I'm trying to get the hang of all this. Thanks!</p>
  
  <pre><code>int main() {
    int* x = malloc(sizeof(*x));
    int* y = malloc(sizeof(*y));
    *x = 2;
    *y = 5;
    x = y;
    *y = 6;
    *x = 4;
    printf("y = %d\n", *y);
  
    free(x);
    free(y);
  
    return 0;
  }
  </code></pre>