I am trying the following simple CUDA C code but getting garbage value printed:
#include <stdio.h>
__global__ void hellocuda(int* tidx){
*tidx = 100;
}
int main(){
int* d_tidx;
int* c;
cudaMalloc((void**) &d_tidx, 1);
c = (int*)malloc(sizeof(int));
hellocuda<<<1,1>>>(d_tidx);
cudaMemcpy(c, d_tidx, sizeof(int), cudaMemcpyDeviceToHost);
printf("%d\n", *c);
return EXIT_SUCCESS;
}
The output I am getting on this is: -273093020
Could someone help debugging this code. Thanks.
This line
printf("%d\n", *c);
pulls sizeof(*c)
bytes from the stack, from which only the first byte is valid memory, as allocated by:
int * c;
...
c = (int*) malloc(1);
As sizeof(*c)
definitly is larger then 1
the call to printf()
above provokes undeinfed behaviuor.
Allthough I cannot really parse this:
cudaMalloc((void**) &d_tidx, 1);
The 1
looks obscure to me.