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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top