0xC0000005: Access violation executing location 0x00000000. When trying to access the insert function of multimap

白昼怎懂夜的黑 提交于 2019-12-12 03:38:26

问题


I was coding for one of the problems in USACO Training Page, and when I tried to debug my code:

#include <iostream>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
using namespace std;
int translate(string &s) {
    int n = s.length();
    int result = 0;
    for (int i = 0; i < s.length(); i++) {
        int n1 = 0;
        if (s.at(i) == 'Q' || s.at(i) == 'Z') {
            return -1;
        }
        switch (s.at(i)) {
        case 'A':
        case 'B':
        case'C':
            result = result + 2 * pow(10, n-1);
            n--;
            break;
        case 'D':
        case 'E':
        case'F':
            result = result + 3 * pow(10, n - 1);
            n--;
            break;
        case 'G':
        case 'H':
        case 'I':
            result = result + 4 * pow(10, n - 1);
            n--;
            break;
        case 'J':
        case 'K':
        case 'L':
            result = result + 5 * pow(10, n - 1);
            n--;
            break;
        case 'M':
        case 'N':
        case 'O':
            result = result + 6 * pow(10, n - 1);
            n--;
            break;
        case 'P':
        case 'R':
        case 'S':
            result = result + 7 * pow(10, n - 1);
            n--;
            break;
        case 'T':
        case 'U':
        case 'V':
            result = result + 8 * pow(10, n - 1);
            n--;
            break;
        case 'W':
        case 'X':
        case 'Y':
            result = result + 9 * pow(10, n - 1);
            n--;
            break;
    }
    }
    return result;
}
bool mycompare(int n, int m) {
    string a, b;
    a = to_string(n);
    b = to_string(m);
    if (a < b) {
        return true;
    }
    else return false;
}
int main() {
    bool(*ptr)(int, int);
    typedef multimap<int, string, bool(*)(int, int)> mmid;
    mmid pairs(ptr);
    string s1;
    ifstream inFile("dict.txt", ios::in | ios::binary);
    while (cin>>s1) {
        int f =translate(s1);
        pairs.insert(mmid::value_type(f, s1));
    }
    int m;
    cin >> m;
    multimap<int, string>::iterator it;
    while (true) {
        it = pairs.find(m);
        if (it != pairs.end()) {
            cout << it->second << endl;
            pairs.erase(it);
        }
        else {
            break;
        }
    }


    return 0;
}

When I try to enter"ANDY HARRY", when it processes to the second entry,

pairs.insert(mmid::value_type(f, s1));

At this line it gives me the code of 0xC0000005: Access violation executing location 0x00000000. What is wrong with my code? And why is it giving me the error message not on the first entry but on the second one? Thanks.


回答1:


I think the issue is in this code:

bool(*ptr)(int, int);
mmid pairs(ptr);

In the first line, you define a function pointer named ptr, but you leave it uninitialized. This means that when you initialize pairs with ptr, you're initializing it with a garbage comparison function pointer.

To fix this, pass in the actual comparison function you want to use. For example:

mmid pairs(mycompare);

Additionally, this code is quite suspicious:

multimap<int, string>::iterator it;
it = pairs.find(m);

Notice that the type of the multimap doesn't match the type of pairs, so there's no a priori reason to suspect this iterator would work correctly. Consider this instead:

mmid::iterator it;


来源:https://stackoverflow.com/questions/38254209/0xc0000005-access-violation-executing-location-0x00000000-when-trying-to-acces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!