     CudaSafeCall( cudaMemcpy(dev_docIds, p.docIds, sizeof(int) * p.docsLength, cudaMemcpyHostToDevice) );
     CudaSafeCall( cudaMemcpy(dev_weights, p.weights, sizeof(float) * p.docsLength, cudaMemcpyHostToDevice) );
 
+    dev_postings_docIds_pointers[i] = dev_docIds;
+    dev_postings_weights_pointers[i] = dev_weights;
     free(p.weights); free(p.docIds);
   }
 }

  if (!terms && !docs) return;

  // free docs norm
  printf("Cuda: Deallocating old docs norm memory\n");
  cudaFree(dev_docsNorm);

  // free postings
  printf("Cuda: Deallocating old postings from memory\n");
  // WARNING: THIS PROBABLY WILL NOT DEALLOCATE POSTINGS COMPLETELY
  // NOT TESTED
  int i; for(i=0; i<terms; i++){
    cudaFree(&dev_postings[i].docIds);
    cudaFree(&dev_postings[i].weights);
  }
  cudaFree(dev_postings);
}

  CudaSafeCall( cudaMalloc((void**)&dev_postings, sizeof(PostingTfIdf) * terms) );
  CudaSafeCall( cudaMemcpy(dev_postings, postings, sizeof(PostingTfIdf) * terms, cudaMemcpyHostToDevice) );

  // these variables saves all pointers in postings
  // needed to deallocate postings from gpu memory
  dev_postings_docIds_pointers = (int **) malloc(sizeof(int *) * terms);
  dev_postings_weights_pointers = (float **) malloc(sizeof(float *) * terms);

  int i;
  int *dev_docIds;
  float *dev_weights;
