C++;向量问题 我在C++中用向量获得了一些奇怪的行为,我希望有人能帮助我。我有这样一个向量: vector<Instruction*> allInstrs;

C++;向量问题 我在C++中用向量获得了一些奇怪的行为,我希望有人能帮助我。我有这样一个向量: vector<Instruction*> allInstrs;,c++,vector,C++,Vector,我遇到的问题是: 我需要遍历每条指令,提取特定字段,并使用这些字段对向量中的指令进行一些分析。要做到这一点,我基本上是这样做的 VariableList Variables; void GenerateVariableList() { for (int i = 0; i < allInstrs.size(); i++) { Variables.Add(allInstrs[i]); }

我遇到的问题是:

我需要遍历每条指令,提取特定字段,并使用这些字段对向量中的指令进行一些分析。要做到这一点,我基本上是这样做的

VariableList Variables;

void GenerateVariableList()
    {
        for (int i = 0; i < allInstrs.size(); i++)
        {           
             Variables.Add(allInstrs[i]);
        }
        Variables.RemoveDuplicates();
    }
变量列表变量;
void GenerateVariableList()
{
对于(int i=0;i
变量列表定义为

struct VariableList
{
    void Add(simple_instr* instr)
    {
        PrintOpcode(instr);
        switch(instr->opcode)
            {
                case STR_OP:
                case MCPY_OP:
                    Add(instr->u.base.src1);
                    Add(instr->u.base.src2);
                    break;              
                case LDC_OP: 
                    Add(instr->u.ldc.dst);
                    break;              
                case BTRUE_OP:
                case BFALSE_OP: 
                    Add(instr->u.bj.src);
                    break;              

                case CALL_OP:
                    cout << "CALL OP" <<endl;
                    break;

                case MBR_OP:                    
                    Add(instr->u.mbr.src);
                    break;          

                case RET_OP:
                    if (instr->u.base.src1 != NO_REGISTER)
                        Add(instr->u.base.src1);
                    break;              

                case CVT_OP:
                case CPY_OP:
                case NEG_OP:
                case NOT_OP: 
                case LOAD_OP: 
                    Add(instr->u.base.dst);
                    Add(instr->u.base.src1);
                    break;

                case LABEL_OP:
                case JMP_OP:
                    break;                      

                default:
                    Add(instr->u.base.dst);
                    Add(instr->u.base.src1);
                    Add(instr->u.base.src2);
                    break;

            }
    }

    void Add(Variable var)
    {
        variableList.push_back(var);
    }

    void RemoveDuplicates()
    {
        if (variableList.size() > 0)
        {
            variableList.erase(unique(variableList.begin(), variableList.end()), variableList.end());
            currentID = variableList.size();
        }
    }

    VariableList()
    {
        currentID = 0;
    }

    VariableList(VariableList& varList, bool setLiveness = false, bool LiveVal = false)
    {
        currentID = 0;
        for (int i = 0; i < varList.size(); i++)
        {
            Variable var(varList[i]);
            if (setLiveness)
            {
                var.isLive = LiveVal;
            }
            variableList.push_back(var);
        }
    }

    Variable& operator[] (int i)
    {
        return variableList[i];
    }

    int size()
    {
        return variableList.size();
    }

    vector<Variable>::iterator begin()
    {
        return variableList.begin();
    }

    vector<Variable>::iterator end()
    {
        return variableList.end();
    }

    protected:
        int currentID;
        vector<Variable> variableList;

        void Add(simple_reg* reg, bool checkForDuplicates = false)
        {   cout << "Register Check" <<endl;
            if (reg == null)
            {
                cout << "null detected" << endl;
                return;
            }
            if (reg->kind == PSEUDO_REG)
            {   

                if (!checkForDuplicates || (checkForDuplicates && find(variableList.begin(), variableList.end(), reg->num) != variableList.end()))
                {
                    cout << "Adding... Reg  " << reg->num << endl;
                    Variable var(reg->num, currentID);

                    variableList.push_back(var);
                    currentID++;
                }
            }
        }

};
结构变量列表 { 无效添加(简单安装*安装) { 打印操作码(instr); 开关(仪表->操作码) { 案例STRU OP: 案例MCPY_OP: 添加(仪表->u.base.src1); 添加(instr->u.base.src2); 打破 案例LDC_OP: 添加(仪表->u.ldc.dst); 打破 案例B真实操作: 案例B假手术: 添加(仪表->u.bj.src); 打破 个案电话: cout u.base.src1); 打破 案例CVT_OP: 案例CPY_OP: 案例NEG_OP: 不适用于以下情况: 壳体载荷_OP: 添加(仪表->u.base.dst); 添加(仪表->u.base.src1); 打破 案例标签(OP): 案例JMP_OP: 打破 违约: 添加(仪表->u.base.dst); 添加(仪表->u.base.src1); 添加(instr->u.base.src2); 打破 } } 无效添加(变量var) { 变量列表。推回(var); } 已删除的无效副本() { if(variableList.size()>0) { 擦除(唯一的(variableList.begin()、variableList.end()、variableList.end()); currentID=variableList.size(); } } 可变列表() { currentID=0; } VariableList(VariableList&varList,bool setLiveness=false,bool LiveVal=false) { currentID=0; 对于(int i=0;i
但在阅读了答案后,我把它改回使用“new”现在它可以正常工作了。感谢您的帮助,很抱歉这个愚蠢的问题,很可能传递给构造函数的
常量simple\u instr&simple
超出了范围,并且您保留了一个指向simple\u instr的无效引用/指针。

可能与您的问题无关,但肯定是一个奇怪行为的潜在来源:Your
指令(const simple\u instr&simple)
构造函数可能会在您不打算调用时被调用。请将其标记为显式

explicit Instruction(const simple_instr& simple) ...

如果这会导致编译器错误,那么这就是进展:-)您可能需要编写一个复制构造函数来消除这些错误,并在需要的地方显式调用旧构造函数。

因此,有几个可疑的观察结果:

  • 变量列表
    的定义中,您使用了一个名为
    变量
    的类型-该类型是如何定义的
  • 应使用迭代器对容器进行迭代:

for(vector::iterator it=allInstrs.begin();
it!=allInstrs.end();
++(it){
变量。添加(*it);
}

    你应该考虑使用向量,或者用矢量来代替指针。
我可以给你一个与你的代码相关的“不要”的大致概述

  • 在这种情况下,您使用从simple_instr“派生”的类是正确的,但您这样做是错误的,因为稍后您会执行基于类型的switch语句。基于类型(而不是状态)的switch语句是一种反模式。您应该调用基类的某个虚拟方法

  • 几乎可以肯定的是,您不希望派生类从基类复制。您希望使用参数来构造它以构造其基类

  • 您需要一个基类指针的向量?并管理可能共享的生命周期\u ptr

  • const正确性。某些方法,如size()肯定应该是const。对于其他方法,可能需要两个重载


为了完整起见,您可以给我们打印操作码代码吗?谢谢。您还可以显示填充
allInstrs
向量的代码吗?您正在集合中存储指针,因此将其更改为使用智能指针可能是个好主意。请尝试将您发布的代码限制为复制错误。这太长了。请尝试使用调试器跟踪该错误。这似乎是一个简单的问题,但您是否已单步执行了代码并检查了y时的内存
void GenerateVariableList()
    {
        for (int i = 0; i < allInstrs.size(); i++)
        {   
            PrintOpcode(allInstrs[i]);
            Variables.Add(allInstrs[i]);
        }
        Variables.RemoveDuplicates();
    }
static vector<Instruction*> ConvertLinkedListToVector(simple_instr* instructionList)
    {
        vector<Instruction*> convertedInstructions;
        int count = 0;
        for (simple_instr* current = instructionList; current; count++, current = current->next)
        {
            //Instruction* inst = new Instruction(*current);
            Instruction inst = Instruction(*current);
            inst.Id = count;
            convertedInstructions.push_back(&inst);
        }
        return convertedInstructions;
    }
explicit Instruction(const simple_instr& simple) ...
for (vector<Intruction *>::iterator it = allInstrs.begin(); 
     it != allInstrs.end();
     ++it) {
    Variables.Add(*it);
}