First of all i don't get it that if we have to use a double pointer, then why create a normal pointer and then cast it using void**? Why not simply create a double pointer in the first place?
Secondly why do we have to pass a pointer to accept the pointer returned by cudamalloc? Why can't we directly use the pointer that is returned by cudamalloc?
I completely understand how malloc works. I also get it that unlike malloc, cuda returns error code so the pointer is passed as reference. But i don't get anything beyond that?
Could you please explain everything about cudamalloc from scratch?
#include <iostream>
#include "book.h"
global void add( int a, int b, int c )
{ *c = a + b; }
int main( void )
{
int c;
int *dev_c;
cudaMalloc( (void**)&dev_c, sizeof(int));
add<<<1,1>>>( 2, 7, dev_c );
cudaMemcpy( &c, dev_c, sizeof(int),
cudaMemcpyDeviceToHost );
printf( "2 + 7 = %d\n", c );
cudaFree( dev_c );
return 0;
}