C ++ EXC_BAD_ACCESS是什么意思?

时间:2019-02-16 17:30:24

标签: c++ segmentation-fault exc-bad-access

我正在尝试用C ++编写我的第一个神经网络,但遇到了麻烦。 基本上我得到

  

分段错误11

所以我尝试调试,结果发现在第41行之后,该程序由于名为

的原因而停止了
  

EXC_BAD_ACCESS,

是什么意思?我该如何扭转呢?我从其他帖子中看到,分段错误意味着堆栈用完了,这将是一个问题,因为这可能是我计划编写的代码的30%

无论如何这是我的代码,我在注释中输入了第41行(请参阅注释)

 #include <vector>
 #include <iostream>
 #include <cstdlib>
 #include <cassert>
 #include <cmath>
 #include <fstream>
 #include <sstream>

using namespace std;

struct Connection
{
    double w;
    double dw;
};

class Neuron;

typedef vector<Neuron> Layer;
//*************************** class Neuron ***************
class Neuron
{
public:
    Neuron(unsigned numOutput, unsigned MyIndex);
    void setOtpuVal(double Val){m_outputVal = Val; }
    double getOutputval(void) const {return m_outputVal; }
    void feedForward (const Layer &prevLayer);

private:
    static double randomWeight(void) {return (rand()/(RAND_MAX));}
    unsigned  m_MyIndex;
    double m_outputVal;
    vector<Connection> m_outputWeights;

};
void Neuron::feedForward (const Layer &prevLayer){

    double sum = 0.0; //<===================================that's line 40
    for (unsigned i = 0; i < prevLayer.size(); ++i)
    {
        sum += prevLayer[i].getOutputval() * prevLayer[i].m_outputWeights[m_MyIndex].w; 
    }
}



Neuron::Neuron(unsigned numOutput, unsigned MyIndex){

for (unsigned c = 0; c < numOutput; ++c)
    {
        m_outputWeights.push_back(Connection());
        m_outputWeights.back().w = randomWeight();
    }

    m_MyIndex = MyIndex;

}





//*************************** class Net    *********************
class Net{

    public:
        Net (const vector<unsigned> &Topology);
        void feedForward(const vector<double> &Input );
        void backProp (const vector<double> &Target){};
        void getResults(vector<double> &Output) const{}; //const non modifica l'oggetto
    private:
        std::vector<Layer> m_layer; // m_layer []   

};

void Net::feedForward (const vector<double> &Input ) {

//assert(Input.size() == m_layer[0].size()-1);
// assert(inputVals.size() == m_layers[0].size() - 1);

    //feeding
    for (unsigned i = 0; i < Input.size()-1; ++i) {
        m_layer [0] [i].setOtpuVal(Input[i]);

    }

    //foorward propagatin
    for (unsigned i = 0; i < m_layer.size(); ++i)
    {
        Layer &prevLayer = m_layer[i-1];
        for (unsigned j = 0; j < m_layer[i].size(); ++j)
        {
            m_layer[i][j].feedForward(prevLayer);   
        }
    }
}
Net::Net (const vector<unsigned> &Topology){

    unsigned numLayer = Topology.size();
    for (unsigned i = 0; i < numLayer; ++i)
    {
        m_layer.push_back(Layer());
        unsigned numOutput = i == Topology.size() - 1 ? 0 : Topology[i +1]; //inportante!!!!
        for (unsigned j = 0; j <= Topology[i]; ++j)
        {
            m_layer.back().push_back(Neuron(numOutput,j));
            cout<<"made a Neuron"<<endl;
        }
    }

}

int main(int argc, char const *argv[])
{
    ifstream reader("Istruzioni.txt",ios::in);
    std::vector<double> Input;
    std::vector<double> Target;
    std::vector<double> Output;
    std::vector<unsigned> Topology;

    Topology.push_back(3);
    Topology.push_back(2);
    Topology.push_back(1);
    Net MyNet(Topology);


    char letter;
    for (int i = 0; i < 4; ++i)
    {
        reader.get(letter);
        if (i == 3)
        {
            Target.push_back( (float)letter - 48);
        }
        else 
            Input.push_back((float)letter - 48);

    }
    reader.close();



    MyNet.feedForward (Input);
    MyNet.backProp(Target);
    MyNet.getResults(Output);


    return 0;
} 

1 个答案:

答案 0 :(得分:0)

错误消息和行为通常是无效的内存访问。这是UB的可能症状之一。因此,此后可以说的只是一个可能的解释,而不能肯定。

查看代码,并假定使用有效的prevLayer参数对其进行了调用:

for (unsigned i = 0; i < prevLayer.size(); ++i)
{
    sum += prevLayer[i].getOutputval() * prevLayer[i].m_outputWeights[m_MyIndex].w; 
}

很有可能超出范围。由于我原则上处于范围之内,所以麻烦来了m_MyIndex

该怎么办?

  • 检查该成员是否已正确初始化。
  • 检查其值是否为正且小于prevLayer[i].m_outputWeights.size()
  • 如果这不能帮助解决问题,则意味着代码中的其他地方混乱了(例如prevLayer已经引用了一个不存在的对象,或者之前发生了一些内存损坏并且没有显示任何内容)后果直到这一刻)。