![](/img/trans.png)
[英]Visual Studio 2010, C++, “vector iterators Incompatible” during vector.clear()
[英]Visual C++ 2010 map/set iterators incompatible
我正在用這個錯誤拔頭發。 該功能可以在符號表中查找8080仿真器中調試器的地址。 想法是這樣的:我有一個帶有地址和標簽的符號表,並且此函數采用一個地址,並嘗試在給定距離(通常為10)內找到最接近的符號。 然后,它返回一個字符串,例如“ label”或“ label + 2”。 如果找不到足夠接近的匹配項,則返回NULL。
一切正常,直到仿真CPU執行了跳轉之后,Visual C ++才在for循環的行上聲明“ map / set迭代器不兼容”。 我已經看到了與此相關的線程問題,它是一個多線程應用程序,但是符號映射僅在一個線程內使用,一旦在啟動時被填充,就不會被其他讀取。
我嘗試了while循環,for循環,每個循環,無論有沒有自動迭代器。 我嘗試取出循環內的返回值,並通過設置符號和距離進行中斷,以在循環外的底部返回完全匹配項。 它仍然在同一時間cho住。 (地址為1418,但是該函數在返回完全匹配或NULL之前已被調用六次)。
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <stdio.h>
#include <string>
#include <map>
using namespace std;
map <int, string> symbol_list;
/* -- function that fills the table -- */
const char * look_up_symbol(int address, int max_distance){
static char buffer[32];
auto symbol = symbol_list.end();
int distance; // distance to the current found symbol
int c_distance; // current distance
distance = 65536; // more than memory, bigger than big
for (auto p = symbol_list.begin(); p != symbol_list.end(); ++p){
if (address == p->first){
// found an exact match
strcpy(buffer, p->second.c_str());
return buffer;
}
c_distance = abs(address - p->first);
if (c_distance <= max_distance){
// we've found one close enough to consider
if (c_distance < distance){
// closer then the closest one considered yet.
symbol = p;
distance = c_distance;
}
}
}
if (symbol == symbol_list.end()){
// we didn't find one close enough
return NULL;
}
sprintf(buffer, "%s%+d", symbol->second.c_str(), distance);
return buffer;
return NULL;
}
這確實讓我感到難過...我不確定p是如何匹配symbol_list.end()的,因為它是一種自動類型,並且沒有代碼可以在填充symbol_list之后實際更改其內容。
事實證明,它是從使用原始字符串的危險。 我通常會很好地控制它們,但是一個調用堆棧(在反匯編函數中),接受反匯編指令的c字符串太小。 如最初創建的那樣,它最多只需要保留9個字節加null(以“ SHLD 1F00”為例),當它遇到更長的指令時,就會引起問題。 在此函數內,最好使用32個字節,因為從文件中讀取它們的函數僅抓取23個字節,這比屏幕上允許顯示的21個字節還多。
我會並且確實使用std :: string,但是對於某些事情,C風格的字符串對我來說更舒服。 再一次,我會盡最大的努力對待他們。 但是,這個特定項目的開發周期很短,並且會長時間收集灰塵,如果將其與危險的編碼方法結合使用,效果會不佳。 活着學習。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.