I have a quick question that I can’t seem to find online.
I am using CUDA to do some GPU work, and I need some data allocated on the GPU. The cudaMalloc function goes like this:
cudaMalloc(void** identifier, size_t space);
Easy enough. So, let’s allocate an integer.
int i = 5;
cudaMalloc((void**)&(&i), sizeof(int));
But this errors (“expression must be an lvalue or a function designator”). The apparent workaround is to declare i as a pointer to begin with, and then take the address of it, and that works perfectly fine; I just hate workarounds.
I feel like this question should have an obvious answer – after all, the **, *** and even ********** work just fine in C. So, how do I get the address of the address of a variable ‘cleanly’?
Thanks!
That’s not a workaround; that’s the right way to do it.
The function wants the address of a pointer to
int, because it’s going to set that pointer to point to anintthat it has just allocated. Therefore you need the address of a real, allocated pointer. An expression like “&&i” asks the compiler to give you the address of i — which is a pure number, with no storage location — and then give you a pointer to that value, which of course it can’t do.So you want to say
Now
*pis theintthat was allocated.