Segmentation fault at CudaMalloc

Viewed 44

I am writing this code for CUDA. I am facing Segfault at CudaMalloc for int* at d_runs

    int* d_runs;   
    BYTE* d_min;
    BYTE* d_max;
    BYTE* d_rawDataByte[size];

    /Allocate space for device copies
    cudaMalloc((void**)&d_runs, size*sizeof(int));
    cudaMemcpy(d_runs, &_runs[0], size*sizeof(int), cudaMemcpyHostToDevice);

    cudaMalloc((void **)&d_min, size*sizeof(BYTE));
    cudaMemcpy(d_min, _min, size*sizeof(BYTE), cudaMemcpyHostToDevice);

    cudaMalloc((void **)&d_max, size*sizeof(BYTE));
    cudaMemcpy(d_max, _max, size*sizeof(BYTE), cudaMemcpyHostToDevice);
    cudaMalloc((void**)&d_rawDataByte, size*sizeof(BYTE *));

size=1000.

Edit: Ok. I have changed BYTE* d_rawDataByte[size] to BYTE** d_rawDataByte

cudaError_t rc = cudaMalloc((void **)&d_min, size*sizeof(BYTE));
    if (rc != cudaSuccess)
        std::cout<<"Could not allocate memory: " <<rc;
    cudaMemcpy(d_min, _min, size*sizeof(BYTE), cudaMemcpyHostToDevice);

Still the problem persists. Infact, none of the cudaMalloc is behaving properly. I am getting Segfault in every one of them.

1 Answers

You should check the result of cudaMalloc to see if it was successful.

This is clearly wrong:

cudaMalloc((void**)&d_rawDataByte, size*sizeof(BYTE *));

d_rawDataByte was declared as an array, so you can't reassign its address to point somewhere else. It would appear that you intended to allocate an array of pointers? In that case it should have been BYTE** d_rawDataByte;

Related