This is the sample version of my project.  It throws a run-time error because I could not clear the contents and already allocated dynamic memory of data array in the bipartition_fn() function. 
Can someone please help me with clearing the contents of data in the bipartition structure after every reoccurrence of bipartition_fn() in the loop inside kdtree_fn()
#include <stdio.h>
#include <stdlib.h>
typedef struct{
    int **data;
}kdtree;
typedef struct{
    int *data;
}bipartition;
void kdtree_fn(int *data, bipartition bp);
bipartition bipartition_fn(int *data);
int main(){
  int i;
  int *data;
  data = malloc(4*sizeof(int));
  for(i=0; i<4;i++)
    data[i] = i;
  bipartition bp;
  kdtree kd;
  kdtree_fn(data,bp);
}
void kdtree_fn(int *data, bipartition bp){
  kdtree k1;
  k1.data = malloc(5*4*sizeof(int));
  int i,j;
  for(j=0; j<5; j++){
    bp = bipartition_fn(data);
    for( i=0; i<4; i++){
      k1.data[j][i] = bp.data[i];
      printf("%d ",k1.data[j][i]);
    }
    printf("\n");
  }
  return k1;
}
bipartition bipartition_fn(int *data){
  bipartition bp1;
  int i;
  bp1.data = malloc(4*sizeof(int));
  for(i=0; i<4; i++){
    bp1.data[i] = data[i] +1;
  }
  return bp1;
}
 
     
     
     
    