cudaMalloc structure

I want to cudaMalloc a structure array, and then access it on the device.

Here is what I tried:

__global__ void structker(struct test *ptr){





	struct test{

  int a;

  int b;

	} *ptr, ptr_h[5];

	cudaMalloc((void **) &ptr, sizeof(struct test)*5);



But I get:

(6): error: pointer to incomplete class type is not allowed

(19): error: argument of type “test *” is incompatible with parameter of type “test *”

(6) is


and (19) is



I think you are not allowed to give the kernel a struct as argument.

Thanks for the reply. So do you have any idea how I can achieve something like this then?

You have defined you struct inline inside the main function, so it is not visible to the global function. Move you struct definition to the top, and then there should be nothing wrong with your example.

Also, you can also pass small structs by value to global functions. I do this all the time. CUDA will do the CPU->GPU copy for you. For large structs, it is better to use the cudaMalloc() approach as you are doing here.

The argument is a pointer (a pointer to a struct admittedly, but pointer is pointer), not a struct.

At least for the first error:

This has nothing to do with CUDA, this is just a general C question.

The kernel uses “struct test” both before and outside the scope where the exact structure of it is defined (namely in main). Thus the compiler of course can not know which members there are in “struct test” and certainly not how to access them.

I do not know about the second error, is this really the exact error message? Then there is something seriously wrong with the compiler, since the code neither defines nor uses a “test” type, only “struct test”.

Either way, get your code to compile and work with a regular C compiler first, IMO this forum is only about CUDA and not C.

Haha! It works :-)

Moving the struct definition outside main cured both errors.

Regarding the problem really being a C mistake rather than relating to CUDA, I apologise for that. I had actually written a C version too, but doing everything in main, so I suppose that is why I didn’t have error. I just jumped to the conclusion that CUDA was causing the problem.

Thank you very much.