Segmentation fault after 24 iterations

Viewed 64

I am writing this code for which has a large array size = 10000 values

    BYTE** _rawDataByte;

     _rawDataByte = (BYTE**)malloc(size*sizeof(BYTE*));

    size_t counter = 0;
    for(size_t i =0; i<size; i++)
    {   
        //Assign values to device varibles

        //Calculate rawDataByte
        std::string rawData(_bytesRaw);
        
        _rawDataByte[i] = (BYTE*)hex2bytes(rawData.c_str());

        std::cout << ++counter <<std::endl;
    }

    std::cout <<"Step 00 completed" <<std::endl;

So, I am getting segfault at the double pointer _rawDataByte. I am using nvcc compiler on Linux.

1 Answers

This line:

BYTE** _rawDataByte = new BYTE*;

allocates one BYTE*.

This line:

_rawDataByte[i] = ... blah blah ...

writes the i'th BYTE* (starting from 0). If i isn't 0, then it's out of bounds. _runs has the same problem.

Related