exc_bad_access代码1错误

时间:2018-04-19 17:25:58

标签: c++ memory-management

经过一些错误搜索后,我发现我的代码在某一点离开了退出代码11,这是因为代码为1的EXC_BAD_ACCESS错误。经过一些谷歌搜索,我看到这一定必须到期对于一些记忆管理不善,但我对C ++很陌生,对我来说似乎没什么。

代码退出

fieldFuncts[field](string, equal, validOps, length, difficulty, rng, opGen);

在文件中(最后一个函数)

//
// Created by Anthony Monterrosa on 4/17/18.
//

#include "MathExpr.h"
#include <list>
#include <iostream>
#include <random>

std::vector<std::function<void(std::vector<MathExpr::CharType> &, MathExpr::NumType &, std::vector<MathExpr::NumType> &)>> MathExpr::layerFuncts;
std::vector<std::function<void(std::vector<MathExpr::CharType> &, MathExpr::NumType &, std::vector<MathExpr::Op> &, unsigned char, unsigned char, std::mt19937 &, std::uniform_int_distribution<MathExpr::OpType> &)>> MathExpr::fieldFuncts;

void MathExpr::init() {
    initLayerFuncts();
    initFieldFuncts();
}

void MathExpr::initLayerFuncts() {
    layerFuncts.resize(Op::EOOE);

    layerFuncts[static_cast<unsigned long>(Op::addition)]    = [](std::vector<MathExpr::CharType> &string, NumType &equal, std::vector<NumType> & otherArgs) -> void {
    string.insert(string.end(), {' ', opToChar(Op::addition),  ' '});
    equal += otherArgs[0];
    std::vector<MathExpr::CharType> digits = intToDigit(otherArgs[0]);
    for(int i = 0; i < digits.size(); i++ ) {
        string.push_back(digits[i]);
    }
};

layerFuncts[static_cast<unsigned long>(Op::subtraction)] = [](std::vector<MathExpr::CharType> &string, MathExpr::NumType &equal, std::vector<NumType> & otherArgs) -> void {
    string.insert(string.end(), {' ', opToChar(Op::subtraction), ' '});
    equal -= otherArgs[0];
    std::vector<MathExpr::CharType> digits = intToDigit(otherArgs[0]);
    for(int i = 0; i < digits.size(); i++ ) {
        string.push_back(digits[i]);
    }
};
}

void MathExpr::initFieldFuncts() {
    fieldFuncts.resize(Field::EOFE);

    fieldFuncts[static_cast<unsigned long>(Field::integers)] = [](std::vector<MathExpr::CharType> &string, NumType &equal, std::vector<MathExpr::Op> &validOps, unsigned char length, unsigned char difficulty, std::mt19937 &rng, std::uniform_int_distribution<MathExpr::OpType> &opGen) -> void {
    std::uniform_int_distribution<MathExpr::NumType> numGen(1, static_cast<MathExpr::NumType>(pow(10, difficulty)));

    equal = numGen(rng);
    std::vector<MathExpr::CharType> digits = intToDigit(equal);
    for(int i = 0; i < digits.size(); i++ ) {
        string.push_back(digits[i]);
    }
    for (int i = 0; i < length - 1; i++) {
        MathExpr::Op op = validOps[opGen(rng)];
        int count  = otherArgsCount(op);
        std::vector<MathExpr::NumType> otherArgs(count);
        for(int j = 0; j < count; j++) {
            otherArgs[j] = (numGen(rng));
        }
        layer(string, equal, op, otherArgs);
    }
};
}

char MathExpr::opToChar(OpType ordinal) {
    return opToChar(static_cast<Op>(ordinal));
}
char MathExpr::opToChar(Op op) {
    switch(op) {
        case Op::addition    : return '+';
        case Op::subtraction : return '-';
        default              : return '_';
    }
}

 MathExpr::NumType MathExpr::otherArgsCount(MathExpr::Op op) {
    switch(op) {
        case Op::addition    : return 1;
        case Op::subtraction : return 1;
        default              : return 0;
    }
}

std::vector<MathExpr::CharType> MathExpr::intToDigit(MathExpr::NumType num) {
    std::vector<MathExpr::CharType> digits;
    while(num >= 10) {
        digits.insert(digits.begin(),'0' + static_cast<MathExpr::CharType>(num % 10));
        num /= 10;
    } digits.insert(digits.begin(), '0' + static_cast<MathExpr::CharType>(num));

return digits;
}

bool MathExpr::initBool = false;

MathExpr::MathExpr(std::vector<CharType> exp, MathExpr::NumType equal) {
    if(!initBool) init();
    this->string = std::vector<CharType>(exp);
    this->equal = equal;
}

void MathExpr::print(MathExpr &exp) {
    for(int i = 0; i < exp.string.size(); i++) {
        std::cout << exp.string[i];
    }
}

void MathExpr::layer(std::vector<MathExpr::CharType> &string, MathExpr::NumType &equal, MathExpr::Op op, std::vector<MathExpr::NumType> &otherArgs) {
    layerFuncts[op](string, equal, otherArgs);
}

MathExpr MathExpr::generate(std::vector<MathExpr::Op> &validOps, MathExpr::Field field, unsigned char length, unsigned char difficulty) {
    std::vector<MathExpr::CharType> string;

    std::random_device rd;
    std::mt19937 rng(rd());
    std::uniform_int_distribution<MathExpr::OpType> opGen(0, static_cast<MathExpr::OpType>(validOps.size() - 1));

    MathExpr::NumType equal;
    fieldFuncts[field](string, equal, validOps, length, difficulty, rng, opGen);

    return MathExpr::MathExpr(string, equal);
}

这是相应的.h文件

//
// Created by Anthony Monterrosa on 4/17/18.
//

// EO_E -> "end of _ enum".

#ifndef MATHTESTGENERATOR_MATHEXPR_H
#define MATHTESTGENERATOR_MATHEXPR_H

#include <functional>
#include <random>
#include <vector>

class MathExpr {

    public:
        using FieldType = unsigned char;
        using OpType    = unsigned char;
        using NumType   = short int;
        using CharType  = char;

        enum Field : FieldType {
            integers,
            EOFE // rational, real, complex.
        };

        enum Op : OpType {
            addition,
            subtraction,
            EOOE // multiplication, division, absolute value, radical
        };

        explicit MathExpr(std::vector<CharType>, NumType);
        std::vector<CharType> string;
        NumType equal;

        static void print(MathExpr &);
        static MathExpr generate(std::vector<Op> &, Field = Field::integers, unsigned char length = 2, unsigned char difficulty = 1);

    //protected:
        static void init();
        static bool initBool;

        static void layer(std::vector<MathExpr::CharType> &, NumType &, Op, std::vector<NumType> &);
        static NumType otherArgsCount(Op);

        static std::vector<CharType> intToDigit(NumType);

        static char opToChar(OpType);
        static char opToChar(Op);

        static std::vector<std::function<void(std::vector<MathExpr::CharType> &, NumType &, std::vector<NumType> &)>> layerFuncts;
        static void initLayerFuncts();

        static std::vector<std::function<void(std::vector<MathExpr::CharType> &, NumType &, std::vector<MathExpr::Op> &, unsigned char, unsigned char, std::mt19937 &, std::uniform_int_distribution<MathExpr::OpType> &)>> fieldFuncts;
        static void initFieldFuncts();
};

#endif //MATHTESTGENERATOR_MATHEXPR_H

我的直觉说错误与&#34;字符串&#34;矢量,但我不确定如何辨别。我希望能够深入了解这个问题。

0 个答案:

没有答案