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

  • Home
  • SEARCH
  • 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 4019758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:08:40+00:00 2026-05-20T10:08:40+00:00

I have a struct defined as follows struct VariableList { void Add(simple_instr* instr) {

  • 0

I have a struct defined as follows

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:
                    if (instr->u.call.dst != NO_REGISTER)
                    {
                        Add(instr->u.call.dst);
                    }

                    Add(instr->u.call.proc);

                    for (int i = 0; i < instr->u.call.nargs; i++)
                    {
                        Add(instr->u.call.args[i]);
                    }

                    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;
        dynamicallyCreated = false;
    }

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

            variableList.push_back(*var);
        }

        dynamicallyCreated = variableList.size() > 0;
    }

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

    bool CompareLiveness(VariableList &var)
    {
        if(variableList.size() != var.size())
        {
            return false;
        }

        for (int i = 0; i < variableList.size(); i++)
        {
            if(variableList[i].isLive != var[i].isLive)
                return false;
        }
        return true;
    }

    ~VariableList()
    {
        if(dynamicallyCreated)
        {
            for (vector<Variable>::iterator it = variableList.begin(); it < variableList.end(); ++it)
            {
                //delete (&it);
            }
        }
    }

    protected:
        int currentID;
        vector<Variable> variableList;
        bool dynamicallyCreated;
        void Add(simple_reg* reg, bool checkForDuplicates = false)
        {   
            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 = new Variable(reg->num, currentID);              
                    variableList.push_back(*var);
                    currentID++;
                }
            }
        }
};

I’d like to be able to do a statement like this

VariableList varsIn(Variables, true, false);

that will create a deep copy and allow me to change a few properties. As you can see in my struct, I’m currently attempting to do this using

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

        variableList.push_back(*var);
     }

     dynamicallyCreated = variableList.size() > 0;
}

I don’t think this is the right way to do it though. What’s the proper way to do this sort of copying? Is there a way to do it without using new? For reference, the Variable struct is as follows

struct Variable
{
    int id;
    int num;
    bool isLive;
    simple_op opcode;

    Variable()
    {
        id = 0;
    num = 0;
    opcode = NOP_OP;
    vClass = Basic;
    isLive = false;
    }

    Variable(int _num, int _id = 0, simple_op _op = NOP_OP)
    {
        id = _id;
    num = _num;
    opcode = _op;
    vClass = Basic;
    isLive = false;
    }

    VariableClass GetClass()
    {
        return vClass;
    }

    bool operator==(const Variable &var) const 
    {
        return num == var.num;
    }

    bool operator==(const int &x) const
    {
    return x == num;
    }

    protected:
        VariableClass vClass;
};

VariableClass and simple_op are enums

Thanks in advance

  • 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-20T10:08:41+00:00Added an answer on May 20, 2026 at 10:08 am

    Your code is not only doing dynamic allocation unnecessarily, it’s also leaking Variable instances everywhere. Just use an automatic variable, push_back will make a copy:

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

    And take out the destructor, you can’t delete the elements owned by the vector. If they pointed somewhere, sure, but you’re not storing pointers.

    Also, here’s an even better way:

    VariableList(VariableList& other, bool setLiveness = false, bool LiveVal = false)
        : currentID(0)
        , variableList(other.variableList)
    {
        if (setLiveness) {
            for( int i = 0; i < size(); i++ )
                variableList[i].isLive = LiveVal;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct defined like follows as part of an object. I'm trying
I have a struct defined in a header as follows: #define LC_ERR_LEN 300 typedef
In a C struct I have defined a function pointer as follows: typedef struct
I have a set of structs, defined as follows: typedef struct { int index;
So, I have some code, kind of like the following, to add a struct
I have defined the following struct to represent an IPv4 header (up until the
I have defined an object with a CGPoint ivar and associated property as follows...
I have a struct (could be a class) which defined in 'h' file: struct
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have this struct: struct Map { public int Size; public Map ( int

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.