0

I'm getting the title error at runtime inside method SetNameandOp on the lines where I try to add something to the RealList struct. I think I am referencing RealList wrong, but I am not sure how to correct it. Anyone have any thoughts?

Here are the structs in the headerfile:

     class Instructions
    {
    public:

       //methods
        struct myInstruction
        {
            string name;
            string opcode; //opcode of add, addi etc.
            string rs; //2nd paramter
            string rt; //3rd
            string rd; //1st 
            string shamt; //shift amount
            string function; 
            myInstruction* next;
        }InstructionList;
}

class greenCard
{
public:
    //methods

    struct 
    {
        string name;
        string opcode;
        string function;
    } Instructions[29];
}

methods:

Instructions::InputtedInstructions* Instructions::get_instruction(vector<string>& vec, int counter)
{   
    InputtedInstructions* InputList = new InputtedInstructions[counter];
    myInstruction* RealList = new myInstruction[counter];
    while (ListPosition != counter)
    {

        string text = vec.at(ListPosition);
        istringstream iss(text);
        string command, arg1, arg2, arg3;

        int CommaAmount = count(text.begin(), text.end(), ',');

        if (CommaAmount == 2)
        {
            while( iss >> command >> arg1 >> arg2 >> arg3)
            {
                setNameandOp(RealList, InputList[ListPosition].name, counter);
                ListPosition++;
            }
        }
    }

    return InputList;
}   

void Instructions::setNameandOp(myInstruction* RealList, string set, int counter)
{
    greenCard NameSet;
    int y = NameSet.instructionsearch(set);
    if (y != -1)
    {
        RealList[counter].name = NameSet.Instructions[y].name;
        RealList[counter].opcode = NameSet.Instructions[y].opcode;
        RealList[counter].function = NameSet.Instructions[y].function;
    }


}

int greenCard::instructionsearch(string realinstruction)
{
    int i;
    for (i=0; i < 29; i++)
    {
        if ( realinstruction.compare(Instructions[i].name) == 0)
            return i; 
    }
    return -1;
}

if anything is unclear let me know. Id be more than happy to explain deeper if need be. I just am not sure what else to put without questions.

cmonnats23
  • 15
  • 1
  • 6
  • In c++ array indexing starts with 0 and goes to numOfElements-1. When you're allocating an array of size counter, and referencing counter-th element, you're out of array bounds. – Evgeny Shavlyugin Oct 03 '13 at 06:27
  • so your saying where I instantiate the RealList just put [counter-1] instead of counter? I feel like that wouldnt solve the issue. – cmonnats23 Oct 03 '13 at 06:29
  • @cmonnats23 Probably not, because I feel the code is confused. But Evgeny is right immedaite issue is that you are accessing an array out of bounds. The bigger issue is that the code needs rewriting so that it does whatever it is that you intend it to do. – john Oct 03 '13 at 06:31
  • You have a variable called `counter` but you are not using it to count anything, instead it's the size of two arrays you are allocating. That's the kind of confusion I'm talking about. – john Oct 03 '13 at 06:33
  • counter is coming from the main. its being passed in to get_instructions method from the main. – cmonnats23 Oct 03 '13 at 06:36
  • Yes but my point is that there's no logic in the way you are using it, and you can tell that just from the name. – john Oct 03 '13 at 06:38
  • I can't make any sense of `NameSet.instructionsearch(set);` either, that is always going to return -1, because there's nothing in `NameSet` (you just declared it on the line before). – john Oct 03 '13 at 06:39
  • Sorry I can't give more positive help. – john Oct 03 '13 at 06:41
  • I should append my code up there to show wha that does... but ill attempt to explain it here. all `NameSet` does is allows you to access instructionsearch method (method is there further down in code) which checks the array of structs for a matching string. if there is a matching string return 1. – cmonnats23 Oct 03 '13 at 06:44
  • @john You're doing the best you can, and I appreciate it. – cmonnats23 Oct 03 '13 at 06:46
  • @cmonnats23 I understand that but my point (assuming the code above is the code you really have, you wouldn't be the first person to post code that's different from what you really have) is that `greenCard NameSet;` creates an array of structs with *nothing in it*, so there is no point in searching for a matching string. With this code you are never going to find a matching string. I guess that somewhere else in the code you think you are adding information to `NameSet` but I can tell you (without even seeing the other code) that isn't true. – john Oct 03 '13 at 06:50
  • no, this isnt my full amount of code, I cut a lot out to save other people trouble. I think you may (surprisingly) be wrong on this. whenever I call `greenCard nameset` doesnt it run the constructor? Because in the constructor i have essentially hardcoded the struct. I promises you that it finds something its equal to because when you step into it, it returns a 1 for y. – cmonnats23 Oct 03 '13 at 07:00
  • Is there a way I could pm you about this, or are you not interested/not possible? – cmonnats23 Oct 03 '13 at 07:00

0 Answers0