1

I am recently trying to learn parallel programming in CUDA C language from https://www.udacity.com/course/viewer#!/c-cs344/l-55120467/m-67895450

cudamalloc((void**) &d_in, ARRAY_BYTES);

Can someone please explain what is happening in (void**) &d_in. Also it would be highly appreciated if someone can give me reference to some links or tell me good books where i can learn this kind of advanced C.

Aakash
  • 337
  • 2
  • 4
  • 15
  • 1
    http://stackoverflow.com/questions/7989039/use-of-cudamalloc-why-the-double-pointer I must say I agree on the horrible API design. – this Mar 05 '14 at 22:53
  • possible duplicate of [Why does cudaMalloc() use pointer to pointer?](http://stackoverflow.com/questions/12936986/why-does-cudamalloc-use-pointer-to-pointer) – Robert Crovella Mar 06 '14 at 00:02
  • If you google around, you'll find plenty of resources to learn topics in C or C++ on the web. – Robert Crovella Mar 06 '14 at 00:03
  • @self. Why is it horrible? Is there a better way to do that? – Vitality Mar 06 '14 at 06:20

1 Answers1

2

Actually, it is pure C/C++ matter. This func(&pointer); means that you are passing address (&) of the variable pointer which in fact could be a pointer. So if the variable pointer is declared as int *pointer, expression &pointer has type int ** and the function has to be of type any_type func(int **p)

Yet, the cudaMalloc function has following type:

cudaError_t cudaMalloc(void **d evPtr, size_t size).

It denotes the first argument has to be of the type void **. The type void has special importance in C/C++ and pointers to void (void *) are able to point to any data type. So applying overcasting (void **) in your statement cudamalloc((void**) &d_in, ARRAY_BYTES); only overcasts the address of the pointer to special data type to the address of the pointer to the most general data type.

stuhlo
  • 1,479
  • 9
  • 17