Char数组中的空字符和分段错误 - c ++

时间:2016-02-13 03:18:59

标签: c++ null char

我填充了char数组,但是当我尝试添加字符串的结尾(' \ 0')时,我得到了分段错误。这是我的代码

Plate.h

#ifndef Plate_h
#define Plate_h

#include <string.h>
#include <vector>
#include "OCRChar.h"

using namespace std;

class Plate{
    public:
        Plate();

        void setChars(vector< vector<OCRChar> > chars);
        vector<char*> getPlateNums();
   private:
        vector< vector<OCRChar> > chars;

        vector<char*> plateNums;

        void computePlateNumbers();
        int calculatePlateListSize();
};

#endif

Plate.cpp

#include "Plate.h"

Plate::Plate(){
}


void Plate::setChars(vector< vector<OCRChar> > chars){
     this->chars = chars;
}

vector<char*> Plate::getPlateNums(){
    if(plateNums.size()==0){
        computePlateNumbers();
    }
    return plateNums;
}

void Plate::computePlateNumbers(){
     if(chars.size()== 0)return;
     int platesSize = calculatePlateListSize();
     vector<char*> plates(platesSize);
     int rep = 1;
     for(int i = 0; i<chars.size();i++){
         vector<OCRChar> oChars = chars[i];
         int numCycles = oChars.size()*rep;
         int iterations = platesSize/numCycles;
         for(int j=0;j<iterations;j++){
             for(int k= 0; k<oChars.size();k++){
                 for(int l=0;l<rep;l++){
                     if(!plates[j*oChars.size()*rep+k*rep+l]){
                         plates[j*oChars.size()*rep+k*rep+l] = new char[chars.size()+1];
                     }
                     plates[j*oChars.size()*rep+k*rep+l][i] = oChars[k].getCharacter();
                 }
             }
         }
         rep = numCycles;
     }

     for(int i =0; i<platesSize; i++){
        plates[i][chars.size()] = '\0';
     }

     plateNums = plates;
}

int Plate::calculatePlateListSize(){
    int platesSize = 1;
    for(int i = 0; i<chars.size();i++){
        platesSize *= chars[i].size();
    }
    return platesSize;
}

OCRChar.h

#ifndef OCR_CHAR_h
#define OCR_CHAR_h


class OCRChar{
    private:
        char character;
        float confidence; 
    public:
       OCRChar(char character, float confidence);
       char getCharacter();
       float getConfidence(); 
};

#endif

OCRChar.cpp

#include "OCRChar.h"


OCRChar::OCRChar(char character, float confidence){
    this->character = character;
    this->confidence = confidence;
}

char OCRChar::getCharacter(){
     return character;  
}


float OCRChar::getConfidence(){
    return confidence;
}

的main.cpp

#include "Plate.h"
#include "OCRChar.h"

#include <vector>
#include <iostream>

using namespace std;

int main(void){



    vector< vector<OCRChar> > chars;
    vector<OCRChar> char1;
    vector<OCRChar> char2;
    vector<OCRChar> char3;
    vector<OCRChar> char4;
    vector<OCRChar> char5;
    vector<OCRChar> char6;
    char1.push_back(OCRChar('A',90));
    chars.push_back(char1);

    char2.push_back(OCRChar('1',60));
    char2.push_back(OCRChar('2',50));
    char2.push_back(OCRChar('3',50));
    char2.push_back(OCRChar('4',50));
    chars.push_back(char2);

    char3.push_back(OCRChar('B',60));
    char3.push_back(OCRChar('C',50));
    char3.push_back(OCRChar('D',50));
    char3.push_back(OCRChar('E',50));
    chars.push_back(char3);

    char4.push_back(OCRChar('5',60));
    char4.push_back(OCRChar('6',50));
    char4.push_back(OCRChar('7',50));
    char4.push_back(OCRChar('8',50));
    chars.push_back(char4);

    char5.push_back(OCRChar('F',60));
    chars.push_back(char5);

    char6.push_back(OCRChar('9',60));
    char6.push_back(OCRChar('G',60));
    chars.push_back(char6);

    Plate p;
    p.setChars(chars);
    vector< char*> nums = p.getPlateNums();
    for(int i=0;i<nums.size();i++){
        cout<<"Plate num "<<i<<" :"<<nums[i]<<endl;
    }

}

分段失败在Plate.cpp中的方法computePlateNumbers:

for(int i =0; i<platesSize; i++){
            plates[i][chars.size()] = '\0';
         }

我注意到分段错误恰好发生在Debian / Raspbian Jessie

3 个答案:

答案 0 :(得分:0)

检查platesSize % numCycles == 0。如果不是这种情况,您将不会在plates中初始化plates[j*oChars.size()*rep+k*rep+l] = new char[chars.size()+1];的最后成员。

让我们说

platesSize == 100
oChars.size() == 9
rep == 1
numCycles = oChars.size()*rep == 9
iterations = platesSize/numCycles == 11

j = iterations-1 == 10
k = oChars.size()-1 == 8
l = rep-1 == 0;

j*oChars.size()*rep+k*rep+l == 10*9*1+8*1+0 == 98

因此,您不会初始化plates[99]

答案 1 :(得分:0)

我没有多长时间阅读代码。

但是看错误线,我可以给你一个建议。

plates[i][chars.size()] = '\0';

每当数组元素访问产生问题时,首先要确定的是索引是否在内部。

由于plates是向量,因此您应在此处使用std::vector.at(index)。因此,如果完成任何越界访问,它将抛出。我怀疑是否出界是问题所在。

答案 2 :(得分:0)

我解决了将char *更改为string:

的问题
#ifndef Plate_h
#define Plate_h

#include <string>
#include <vector>
#include "OCRChar.h"

using namespace std;

class Plate{
    public:
        Plate();

        void setChars(vector< vector<OCRChar> > chars);
        vector<string> getPlateNums();
   private:
        vector< vector<OCRChar> > chars;

        vector<string> plateNums;

        void computePlateNumbers();
        int calculatePlateListSize();
};

#endif

Plate.cpp

#include "Plate.h"

Plate::Plate(){
}


void Plate::setChars(vector< vector<OCRChar> > chars){
     this->chars = chars;
}

vector<string> Plate::getPlateNums(){
    if(plateNums.size()==0){
        computePlateNumbers();
    }
    return plateNums;
}

void Plate::computePlateNumbers(){
     if(chars.size()== 0)return;
     int platesSize = calculatePlateListSize();
     vector<string> plates(platesSize);
     int rep = 1;
     for(int i = 0; i<chars.size();i++){
         vector<OCRChar> oChars = chars[i];
         int numCycles = oChars.size()*rep;
         int iterations = platesSize/numCycles;
         for(int j=0;j<iterations;j++){
             for(int k= 0; k<oChars.size();k++){
                 for(int l=0;l<rep;l++){
                     plates[j*oChars.size()*rep+k*rep+l] += oChars[k].getCharacter();
                 }
             }
         }
         rep = numCycles;
     }

     plateNums = plates;
}

int Plate::calculatePlateListSize(){
    int platesSize = 1;
    for(int i = 0; i<chars.size();i++){
        platesSize *= chars[i].size();
    }
    return platesSize;
}