Home > database >  segmentation fault while running C code
segmentation fault while running C code

Time:03-10

I am getting a segmentation fault while running following code :

void convert_fix2float(vector<int8_t*>& resultin, vector<vector<float>>& resultout, const float* output_scale){
    
    cout<<"entering fix to float"<<endl;    
    for (size_t ii = 0; ii < 3; ii  ) {
    size_t width = shapes.outTensorList[ii].width;
    size_t height = shapes.outTensorList[ii].height;
    size_t channel = shapes.outTensorList[ii].channel;
    size_t sizeOut = channel * width * height;
    for (size_t j = 0; j < sizeOut; j  ){
        resultout[ii][j] = resultin[ii][j] * output_scale[ii];
    }
}

cout<<"exiting fix to float"<<endl;

}

If I comment out the statement

    resultout[ii][j] = resultin[ii][j] * output_scale[ii];

segmentation fault disappears. Can someone help me please to identify the cause of the issue.

CodePudding user response:

Segmentation failt just means that no memory was prepared. So one of your elements

  • resultout
  • resultin
  • output_scale

can not be accessed, because no valid memory is accesible at this position.

CodePudding user response:

Operator operator[]() in std::vector can not create new elements. For creating new elements you can use method insert():

resultout.insert(std::vector<float>())
for (size_t j = 0; j < sizeOut; j  )
{
        resultout[ii].insert(resultin[ii][j] * output_scale[ii]);
}
  • Related