title: Why these two C pointers set to NULL after one line code?
body: <p>Here are the codes I do not understand and never met before.
  only one line of codes change rvalue pointer and set it to NULL </p>
  
  <pre><code>if ((*v)-&gt;r != NULL)
              printf("WTF r != NULL r %p p %p p-&gt;l %p (*v) %p \n",(*v)-&gt;r,(*v)-&gt;p,(*v)-&gt;p-&gt;l,(*v));
  
          (*v)-&gt;p-&gt;l = (*v)-&gt;r;
  if ((*v)-&gt;r == NULL)
              printf("WTF r == NULL r %p p %p p-&gt;l %p (*v) %p \n",(*v)-&gt;r,(*v)-&gt;p,(*v)-&gt;p-&gt;l,(*v));
  </code></pre>
  
  <p>both messages were printed. 
  r and l were set to NULL.
  It is very strange.</p>
  
  <p>below are all the codes I try to debug.
  You may compile and run it.
  Thank you very much!!!
  It is a Avl tree</p>
  
  <p>AVL_tree.h</p>
  
  <pre><code>#if !defined AVL_TREE_H
  #define AVL_TREE_H
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;assert.h&gt;
  struct vertex{
     int data;
     long height;
     struct vertex *l,*r,*p;
  };
  void insert(struct vertex** root, int data);
  void* find(struct vertex* root, int data);
  void clear_tree(struct vertex** root);
  void print_tree(struct vertex* root);
  #endif
  </code></pre>
  
  <p>AVL_tree.c</p>
  
  <pre><code>#include"./AVL_tree.h"
  static long
  max(long a, long b)
  {
  return (((a) &gt; (b)) ? a : b);
  }
  static long
  balance_factor(struct vertex *v)
  {
  if (v-&gt;l == NULL &amp;&amp; v-&gt;r == NULL)
      return 0;
  else if (v-&gt;l == NULL)
      return (-1) - v-&gt;r-&gt;height;
  else if (v-&gt;r == NULL)
      return v-&gt;l-&gt;height - (-1);
  
  return (v-&gt;l-&gt;height - v-&gt;r-&gt;height);
  }
  static void
  update_height(struct vertex *v)
  {
  if (v-&gt;l == NULL
      &amp;&amp; v-&gt;r == NULL) {
      v-&gt;height = 0;
      return;
  } else if (v-&gt;l == NULL) {
      v-&gt;height = v-&gt;r-&gt;height + 1;
      return;
  } else if (v-&gt;r == NULL) {
      v-&gt;height = v-&gt;l-&gt;height + 1;
      return;
  }
  v-&gt;height = max(v-&gt;l-&gt;height, v-&gt;r-&gt;height) + 1;
  }
  static void
  right_rotate(struct vertex **v)
  {
  
  if ((*v)-&gt;p != NULL) {
      if ((*v)-&gt;p-&gt;l == (*v))
          (*v)-&gt;p-&gt;l = (*v)-&gt;l;
      else
          (*v)-&gt;p-&gt;r = (*v)-&gt;l;
  }
  struct vertex  *tmp = (*v)-&gt;l-&gt;r;
  (*v)-&gt;l-&gt;r = (*v);
  (*v)-&gt;l-&gt;p = (*v)-&gt;p;
  (*v)-&gt;p = (*v)-&gt;l;
  (*v)-&gt;l = tmp;
  if (tmp != NULL)
      tmp-&gt;p = (*v);
  
  update_height(*v);
  (*v) = (*v)-&gt;p;
  update_height(*v);
  }
  static void
  left_rotate(struct vertex **v)
  {
  
  if ((*v)-&gt;r == NULL)
      printf("1 r == NULL\n");
  
  if ((*v)-&gt;p != NULL) {
      printf(" p is not NULL\n");
      if ((*v)-&gt;p-&gt;r == (*v))
          (*v)-&gt;p-&gt;r = (*v)-&gt;r;
      else if ((*v)-&gt;p-&gt;l == (*v)) {
          printf("p's l  == v\n");
  
          if ((*v)-&gt;r != NULL)
              printf("WTF r != NULL r %p p %p p-&gt;l %p (*v) %p \n",(*v)-&gt;r,(*v)-&gt;p,(*v)-&gt;p-&gt;l,(*v));
  
          (*v)-&gt;p-&gt;l = (*v)-&gt;r;
          if ((*v)-&gt;r == NULL)
              printf("WTF r == NULL r %p p %p p-&gt;l %p (*v) %p \n",(*v)-&gt;r,(*v)-&gt;p,(*v)-&gt;p-&gt;l,(*v));
  
      } else {
          printf("error case \n");
      }
  }
  printf("get there before\n");
  
  if ((*v)-&gt;r == NULL)
      printf("r == NULL\n");
  
  struct vertex  *tmp = (*v)-&gt;r-&gt;l;
  printf("get there after\n");
  (*v)-&gt;r-&gt;l = (*v);
  printf("get there \n");
  (*v)-&gt;r-&gt;p = (*v)-&gt;p;
  (*v)-&gt;p = (*v)-&gt;r;
  (*v)-&gt;r = tmp;
  printf("get there \n");
  if (tmp != NULL)
      tmp-&gt;p = (*v);
  printf("get there \n");
  update_height(*v);
  (*v) = (*v)-&gt;p;
  update_height(*v);
  }
  
  static void
  insert_fixup(struct vertex *new_vertex)
  {
  while ((new_vertex-&gt;p != NULL)
         &amp;&amp; ((new_vertex-&gt;height + 1) &gt; new_vertex-&gt;p-&gt;height)) {
      update_height(new_vertex-&gt;p);
      new_vertex = new_vertex-&gt;p;
      if (balance_factor(new_vertex) == 2) {
          if (balance_factor(new_vertex-&gt;l) == -1) {
              printf("double left right rotating \n");
              left_rotate(&amp;new_vertex-&gt;l);
          }
          right_rotate(&amp;new_vertex);
          return;
      } else if (balance_factor(new_vertex) == -2) {
          if (balance_factor(new_vertex-&gt;r) == 1) {
              printf("double right left rotating \n");
              right_rotate(&amp;new_vertex-&gt;r);
              printf("double right rotating finish\n");
          }
          left_rotate(&amp;new_vertex);
          return;
      }
  }
  }
  void
  insert(struct vertex **tree, int data)
  {
  struct vertex  *new_vertex = malloc(sizeof(struct vertex));
  new_vertex-&gt;data = data;
  new_vertex-&gt;height = 0;
  new_vertex-&gt;l = NULL;
  new_vertex-&gt;r = NULL;
  new_vertex-&gt;p = NULL;
  
  if (tree == NULL)
      return;
  else if (*tree == NULL) {
      *tree = new_vertex;
      return;
  }
  struct vertex  *root = *tree;
  while (1) {
      if (root-&gt;data &lt; data) {
          if (root-&gt;r != NULL)
              root = root-&gt;r;
          else {
              root-&gt;r = new_vertex;
              new_vertex-&gt;p = root;
              break;
          }
      } else {
          if (root-&gt;l != NULL)
              root = root-&gt;l;
          else {
              root-&gt;l = new_vertex;
              new_vertex-&gt;p = root;
              break;
          }
      }
  }
  insert_fixup(new_vertex);
  while ((*tree)-&gt;p != NULL) {
      (*tree) = (*tree)-&gt;p;
  }
  }
  void           *
  find(struct vertex *v, int data)
  {
  while (v != NULL &amp;&amp; v-&gt;data != data)
      if (v-&gt;data &lt; data)
          v = v-&gt;r;
      else
          v = v-&gt;l;
  
  return v;
  
  
  }
  static void
  free_tree(struct vertex *root)
  {
  if (root-&gt;l != NULL)
      free_tree(root-&gt;l);
  if (root-&gt;r != NULL)
      free_tree(root-&gt;r);
  
  free(root);
  return;
  }
  void
  clear_tree(struct vertex **root)
  {
  free_tree(*root);
  *root = NULL;
  }
  </code></pre>
  
  <p>test.c</p>
  
  <pre><code>#include"./AVL_tree.h"
  #include&lt;stdio.h&gt;
  #include&lt;time.h&gt;
  
  int main(){
  
  time_t t;
  srand(time(&amp;t));
  struct vertex* root = NULL;
  int i,j,limit = 1000000,incretment = 500000;
  printf("starting insert %d elements with increment %d in next round\n",limit,incretment); 
  for (j = 0; j &lt;= 100; j++){
    clock_t start,end;
    start = clock();
    for (i = 0; i &lt; limit;i++){
   int random = rand() % 10;
   printf("inserting %d \n",random);
   insert(&amp;root,random);
    }
    end = clock();
    double total = (double)(end - start)/CLOCKS_PER_SEC;
    printf("%f\n",total);
    clear_tree(&amp;root);
  }
  
  }
  </code></pre>