Home > Blockchain >  Segmentation fault after 24 iterations
Segmentation fault after 24 iterations

Time:09-17

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

    int* _runs = new int;
    const BYTE* _min = hex2bytes(min);
    const BYTE* _max = hex2bytes(max);
    BYTE** _rawDataByte = new BYTE*;
    BYTE** _result = new BYTE*;

    //device copies of min, max, rawByte and byteIndex
    int* d_runs = new int; 
    BYTE* d_min = new BYTE;
    BYTE* d_max = new BYTE;
    BYTE** d_rawDataByte = new BYTE*;
    //char** d_result;

    std::cout << size <<std::endl;  size_t counter = 0;
    for(size_t i =0; i<size; i  )
    {   
        //Assign values to device varibles
        _runs[i] = stepSize;

        //Calculate rawDataByte
        double amount = startAmount   stepSize*packetSteps;
        std::string rawData(_bytesRaw);
        
        replace_first(rawData, replaceAmountStr, calcAmountStr(amount));
        
        _rawDataByte[i] = (BYTE*)hex2bytes(rawData.c_str());

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

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

for-loop runs for only 24 times and then gives me segfault. Maybe I am missing something. I am using nvcc compiler on Linux.

CodePudding user response:

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.

CodePudding user response:

I solved this issue by changing it to

    int _runs[size];
    BYTE* _rawDataByte[size];
    BYTE* _result[size];

Thanks to the comment by @463035818_is_not_a_number and answer by user253751.

  • Related