Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4018002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:55:46+00:00 2026-05-20T09:55:46+00:00

I’m getting some weird behavior with a vector in C++ I was hoping someone

  • 0

I’m getting some weird behavior with a vector in C++ I was hoping someone could help me out. I have a vector like so:

vector<Instruction*> allInstrs; 

the struct for Instruction is as follows:

struct Instruction : simple_instr
{
    InstrType type;
    Instruction(const simple_instr& simple) : simple_instr(simple) 
    {
        type = Simple;
        loopHeader = false;
        loopTail = false;
    }
    int Id;
    bool loopHeader;
    bool loopTail;
};

the problem I’m having is this:

I need to iterate through each instruction and pull out specific fields and use those to do some analysis on the instructions in the vector. To do that, I was basically doing

VariableList Variables;

void GenerateVariableList()
    {
        for (int i = 0; i < allInstrs.size(); i++)
        {           
             Variables.Add(allInstrs[i]);
        }
        Variables.RemoveDuplicates();
    }

Variable List is defined as

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++;
                }
            }
        }

};

When I do this though, every instruction goes to the default case statement, even though I knwo for a fact some instructions shouldn’t. If I change GenerateVariableList to

void GenerateVariableList()
    {
        for (int i = 0; i < allInstrs.size(); i++)
        {   
            PrintOpcode(allInstrs[i]);
            Variables.Add(allInstrs[i]);
        }
        Variables.RemoveDuplicates();
    }

so that there is now a second PrintOpCode in addition to the one in Variables.Add, the program behaves correctly. I can’t understand why adding a second PrintOpcode makes it work correctly. All print Opcode is is a function with a switch statement that just prints out a specific string depending on what the value of one of simple_instr’s fields is.

VariableList Variables is contained inside of a separate struct called CFG

If you need more information/code i can provide it. If the answer is obvious I apologize, I don’t program in C++ very often

EDIT:

One of the answers left, deleted now though, got me the fix.

Previously I was doing

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;
    }

to make the vector, but after reading that answer I changed it back to using “new” and it works correctly now. Thanks for the help, sorry for the dumb question heh

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:55:47+00:00Added an answer on May 20, 2026 at 9:55 am

    Most likely the const simple_instr& simple passed to your constructor goes out of scope, and you keep an invalid reference/pointer to a simple_instr.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.