Cuda error: invalid device pointer.

I am trying to do the following:

typedef struct {
float *d;
int size;
} data;

data mydata;
mydata.size=1000;
CUDA_SAFE_CALL(cudaMalloc((void **) mydata.d, sizeof(float)*mydata.size));
CUDA_SAFE_CALL(cudaFree(mydata.d));

but on cudafree I get the invalid device pointer. can anyone help me with this please???

CUDA_SAFE_CALL(cudaMalloc((void **) mydata.d, sizeof(float)*mydata.size));

should be

CUDA_SAFE_CALL(cudaMalloc((void **)&mydata.d, sizeof(float)*mydata.size));

Thanks for your answere, what if I wanna do the following, define the struct as a pointer:

typedef struct {

float *d;

int size;

} data;

data *mydata;

mydata->size=1000;

CUDA_SAFE_CALL(cudaMalloc((void **) mydata->d, sizeof(float)*mydata.size));

CUDA_SAFE_CALL(cudaFree(mydata.d));

I get the error: expression must have class type error on cudaMalloc.

How can I solve this???

data *mydata;

mydata->size=1000;

CUDA_SAFE_CALL(cudaMalloc((void **) &(mydata->d), sizeof(float)*mydata->size));

CUDA_SAFE_CALL(cudaFree(mydata->d));

This is a C problem not a CUDA problem.

‘mydata.size’ is incorrect. It should be ‘mydata->size’.

You’ve also got exactly the same bug as last time - (void **) mydata->d is incorrect and should be (void **)&mydata->d.

It might be an idea to learn a bit more C before diving into CUDA.

oooops, I made a mistake writing the code here. I have actually put the addess sign for the malloc. my mistake was accessing the size. forgot to replace it with “->”. thanks.