Visual C ++ 2010 map / set迭代器不兼容

时间:2014-05-05 21:00:34

标签: c++ visual-studio-2010 stl

我正在用这个错误拉我的头发。它是在8080仿真器中为调试器在符号表中查找地址的功能。这个想法是这样的:我有一个带有地址和标签的符号表,这个函数接受一个地址,并试图找到给定距离内的最近符号(通常为10)。然后它返回一个字符串,如“label”或“label + 2”。如果找不到足够接近的匹配,则返回NULL。

一切正常,直到仿真CPU执行了跳转,然后Visual C ++在for循环的行上断言“map / set iterators incompatible”。我已经看到有关此问题的线程问题,它是一个多线程应用程序,但符号映射仅在一个线程内使用,除了读取之外,一旦它在启动时被填充,就不会被触及。 / p>

我已尝试while循环,for循环,每个循环,所有有或没有自动迭代器。我尝试取出循环中的返回,使用设置符号和距离休息以返回循环外部底部的精确匹配。它仍然在同一点上窒息。 (地址为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填充后实际更改它的内容

1 个答案:

答案 0 :(得分:0)

事实证明, 来自使用原始字符串的危险。我通常非常善于控制它们,但是一个调用堆栈(在disasssembler函数中),接受反汇编指令的c字符串太小了。最初创建时,它最多只需要保存9个字节加上空值(例如“SHLD 1F00”),当它遇到更长的指令时,就会产生问题。在这个函数里面,32个字节很好,因为从文件中读取它们的函数只能抓取23个字节,这超过了屏幕允许显示的21个字节。

我会使用std :: string,但对于某些事情来说,C风格的字符串对我来说更舒服。而且,我会尽最大努力对待他们。但是这个特殊项目的开发时间很短,需要很长时间的灰尘聚集,这在与危险的编码实践混合时并不好。过一个'学习。