Below is a small piece of code that copies 4 elements from an array to the GPU. I really dont understand why cudaMemcpy is throwing an error in this case. Please help
int size = 5;
float *a = (float*)malloc(size * sizeof(float));
a[0] = 1.0;
a[1] = 2.0;
a[2] = 3.0;
a[3] = 4.0;
a[4] = 5.0;
float *g;
cudaMalloc((void**)g, 4 * sizeof(float));
float *tem = a+2;
cudaError_t err = cudaMemcpy(g,a,4 * sizeof(float), cudaMemcpyHostToDevice);
if(err !=0){
printf("Cudamemcpy threw error
");
getchar();
}