Programmer
Programmer

Reputation: 6753

cudaMemcpy throwing error during simple copy

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\n");
    getchar();  
}

Upvotes: 2

Views: 4664

Answers (1)

morispaa
morispaa

Reputation: 311

I think you are missing an ampersand:

cudaMalloc((void**)&g, 4 * sizeof(float));

Upvotes: 6

Related Questions