DP_Corcoran
DP_Corcoran

Reputation: 1

double ** a = malloc() works in c but not in cuda?

I've written a program which works in C but won't compile in CUDA for some reason.

The issue arises here:

int key_size = 1000;

int references = 40;

double **ref_ptr;

ref_ptr = malloc(references * sizeof *ref_ptr);    
for(int i = 0; i<references;i++){
    ref_ptr[i] = malloc(key_size * sizeof *ref_ptr[i]);
}

In C this program compiles just fine, it even compiles with NVCC as a C program (nvcc my_program.c). But when I try to compile it as a CUDA program I get this error.

main.cu(191): error: a value of type "void *" cannot be assigned to an entity of type "double **"
main.cu(193): error: a value of type "void *" cannot be assigned to an entity of type "double *"

I tried casting them as pointers but this began creating a lot of memory access issues for me, and my program works perfectly otherwise when compiled in C. Could you help me think through what could be going on here?

Upvotes: 0

Views: 82

Answers (1)

0xd34dc0de
0xd34dc0de

Reputation: 523

It's because malloc return a void*, you need to cast it to the appropriate pointer.

ref_ptr = (double**)malloc(references * sizeof *ref_ptr);    
for(int i = 0; i<references;i++){
    ref_ptr[i] = (double*)malloc(key_size * sizeof *ref_ptr[i]);
}

Upvotes: 1

Related Questions