Transferring data and saving the device pointer

I have some tables on the CPU that I would like to move over to the GPU for future calculations. The tables are setup ahead of time and don’t change after they are intialized. Therefore, I wanted to move them over as soon as they’re set and come back to them later. I think this is possible, but my device pointer becomes invalid as soon as I return from the function that sets it up.


struct type_a{

   int id;

   type_b *b;


struct type_b{

   int id;



type_a *helper = malloc (sizeof(type_a)*10);

//store ids in helper's id field from a master table on CPU


type_a *main;



extern "C"

allocateArrays(type_a* main, type_a* helper){

  for (0 to 9)





After that code runs, I can still use helper, but main gives me invalid device pointer. I’ve tried cudaMemcpy on helper, which will work, but as soon as I try that on main, it throws the invalid error. Do I have to allocate some dummy CPU memory for main for this to work? Am I just doing something stupid? Is there any way to retain this pointer? Sorry for the long post, but I couldn’t make it much shorter without leaving out stuff.


Just a thought: “main” could be a reserved word of sorts and can therefore not be used like you do (you know… the main() function). Does renaming the variable help?

In the real implementation it is actually named CUDASMG, so I don’t think that is a problem.

Here is a minor update to this problem. Outside of the function where helper is allocated, that device pointer is no longer valid either. The CPU side data stored in there is valid, and can be used in other functions, but the device pointer is no good. Still not sure how to solve this.

Shouldn’t you be passing the address of main into allocateArrays?

Ah yes, looks like I got pointered. Thanks for your help