Map - Unhandled Exception: std::out_of_range

Hi,

I'm trying to implement a function that keeps track of recently used addresses. I've created a vector of maps to assign an incrementing value to each address key. That way, later on I can find the address in the map, and update its value, or find the oldest values. However, every time I go to test my program I get an "unhandled exception" due to "std::out_of_range" at the 'auto search' line:

1
2
3
4
5
6
7
8
9
10
11
12
13
    int key = 0;
    void update(uint32_t address, int index, vector<map<uint32_t, int> >& table)
    {
        auto search = table.at(index).find(address);
        if(it != table.at(index).end())
        {
             table[index][address] = ++key;
        }
        else
        {
             table.at(index).insert(make_pair(address, ++key));
        }
    }


I modified it so it could then determine if the vector at the index location doesn't have any maps presently available to search through. But that produced the same error at the 'if(table.at(index).empty())' line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    int key = 0;
    void update(uint32_t address, int index, vector<map<uint32_t, int> >& table)
    {
        if(table.at(index).empty())
        {
            table.at(index).insert(make_pair(address, key));
        }
        else
        {
            auto search = table.at(index).find(address);
            if(it != table.at(index).end())
            {
                 table[index][address] = ++key;
            }
            else
            {
                 table.at(index).insert(make_pair(address, ++key));
            }
         }
    }


Even when I just try to insert a map at the index location I get the same exception thrown:

1
2
3
4
5
6
    int key = 0;
    void update(uint32_t address, int index, vector<map<uint32_t, int> >& table)
    {
       table.at(index).insert(make_pair(address, ++key));
    }


I tried determining if the table was null, and looked into alternative means of adding maps to my vector. Unfortunately, the options I looked into (assign and push_back) are not available. I also looked into implementing a try-catch block, but I'm not too clear on how to properly program it, especially with the "throw" keyword.

I suspect my issue stems from me trying to access the table, however I don't truly know what's causing my problem or how to fix it. Does anyone have any ideas or suggestions regarding what I'm doing wrong and how to potentially resolve it?

Thank you!
Last edited on
It looks like table.at(index) does not exist. Make a debug output of index and table.size() before accessing to see what values were just before crash.
I added the following lines of code:

1
2
3
4
5
6
7
void update(uint32_t address, uint32_t index, vector<map<uint32_t, int> >& table)
{
	int getSize = table.at(index).size();                                      // Added
	cout << "Table size at index " << index << " is " << getSize << endl;    // Added

	table.at(index).insert(make_pair(address, ++key));
}


And it threw the exception at the first line. I couldn't even get it to go pass the getSize variable. On the other hand, looking at the 'table' in debug mode on my watch list, its size says its 0; Which makes since since I'm trying to add values to it. So the problem is with the table.at(index) statement. Could it be the way its structured?
Last edited on
MiiNiPaa wrote:
Make a debug output of index and table.size()
gradstud wrote:
int getSize = table.at(index).size();
I figured out the problem. Since the size of the vector was 0, and I had it coded to look for a particular index, it turns out there was not index location for it to go to. So I initialized the table to contain a certain number of index locations and it worked. However, now I have a new problem. When I enter the function to add maps, the table size suddenly changes. For example, my table would be set to hold 64 index locations, and its passed by reference to the update function, and then changes from a size 64 to 13132 index locations. I have no idea why or where its getting that values since the function and class are bare-bone and contain 3 functions (none of which do any initialization or calculations).
Nevermind, its working. Turns out that was the initialized value before stepping through to pass the table. Thank you for pointing out that the size and the indexing were the source of my problem.
Topic archived. No new replies allowed.