I try to pass my dynamic array of structs to kernel but it doesn't works. I get - "Segmentation fault (core dumped)"
My code - EDITED
#include <stdio.h>
#include <stdlib.h>
struct Test {
unsigned char *array;
};
__global__ void kernel(Test *dev_test) {
}
int main(void) {
int n = 4;
int size = 5;
unsigned char *array[size];
Test *dev_test;
// allocate for host
Test *test = (Test*)malloc(sizeof(Test)*n);
for(int i = 0; i < n; i++)
test[i].array = (unsigned char*)malloc(size);
// fill data
for(int i=0; i<n; i++) {
unsigned char temp[] = { 'a', 'b', 'c', 'd' , 'e' };
memcpy(test[i].array, temp, size);
}
// allocate for gpu
cudaMalloc((void**)&dev_test, n * sizeof(Test));
for(int i=0; i < n; i++) {
cudaMalloc((void**)&(array[i]), size * sizeof(unsigned char));
cudaMemcpy(&(dev_test[i].array), &(array[i]), sizeof(unsigned char *), cudaMemcpyHostToDevice);
}
kernel<<<1, 1>>>(dev_test);
return 0;
}
How correctly I should allocate gpu memory and copy data to this memory?