简体   繁体   English

C++ 分段错误访问类数据成员而不显式使用指针

[英]C++ Segmentation Fault accessing class data member without explicit use of pointers

Note: Similar questions have been asked, but nearly all that I've found seem to make direct use of a pointer which may not be relevant here注意:已经提出了类似的问题,但我发现的几乎所有问题似乎都直接使用了一个可能与此处无关的指针

For this snippet of code对于这段代码

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Unit{
public:
    string name;
    char value;
    vector<Unit> inner;
    Unit(string Name){
        name=Name;
    }
    Unit(char Value){
        name="Character";
    }
};
    class Instruction{
    public:
        int size;
        string type;
        string value;
        map<string, vector<Unit>> vars;
        vector<Instruction> inner;
        vector<Instruction> outer;
        Instruction(string Type,string Value,vector<Instruction>& Outer,map<string, vector<Unit>> &Vars){
            type=Type;
            value=Value;
            outer=Outer;
            vars=Vars;
            size=0;
        }
        Instruction(string Type,vector<Instruction>& Outer,map<string, vector<Unit>> &Vars){
            type=Type;
            outer=Outer;
            vars=Vars;
            size=0;
        }
        bool matches(vector<Unit> &info,int position=0, int vectLocation=0){
            cout<<value<<'\n'; //Segmentation Fault Occurs Here
            return false;
        }
    };
void breakUp(vector<Unit>&u,string s){
    for(int i=0;i<s.size();i++){
        u.push_back(Unit(s[i]));
    }
}


int main(){
    //String to be matched
    vector<Unit> v;
    breakUp(v,"For the love of tests");
    //Instruction Vector
    vector<Instruction> i;
    //Var Vector
    map<string, vector<Unit>> x;
    //Add initial instruction
    Instruction constant("Constant","",i,x);
    constant.inner.push_back(Instruction("String","For the love of tests",constant.inner,x));

    //Test match
    bool result=i[0].matches(v);
    if(result==true){
        cout<<"Match True\n";
    }else{
        cout<<"Match False\n";
    }

    return 0;
}

I get a segmentation fault when attempting to access one of the data members within the matches function.尝试访问matches函数中的数据成员之一时出现分段错误。 This also occurs when it attempts to print size or type .当它尝试打印sizetype时也会发生这种情况。 I have not been able to discern the exact cause of this memory issue and would appriciate any advice or considerations.我一直无法辨别出此内存问题的确切原因,因此会寻求任何建议或考虑。

The reason for SIGSEGV seems to be SIGSEGV的原因似乎是

bool result=i[0].matches(v);

When you did当你做

vector<Instruction> i;

A new std::vector object was created with size 0.创建了一个大小为 0 的新std::vector对象。

Now, when you try to access the first element by the above statement, it may give Segmentation fault.现在,当您尝试通过上述语句访问第一个元素时,可能会出现 Segmentation 错误。

Suggestion:建议:

1) Use const std::string & instead of std::string in the constructor. 1) 在构造函数中使用const std::string &而不是std::string Using std::string will lead to the string getting copied twice (which won't be good for performance).使用std::string会导致字符串被复制两次(这对性能不利)。

vector<Instruction> i; ... bool result=i[0].matches(v);

i is empty, so accessing i[0] yields undefined behavior. i为空,因此访问i[0]产生未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM