使用 valgrind 的地图中的读取大小错误无效
Invalid read size error in a map using valgrind
我有这个代码来填充表格,但我不断收到分段错误。我正在尽头试图找到错误可能是什么。我的函数接收 2 个映射并遍历它们以找到公共字符串。它获取这些公共字符串的 int 值并将其放入表中以计算公共字符串的次数。
myMap findTable(mapGraph * dbgraph1, mapGraph * dbgraph2)
{
typedef mapGraph::const_iterator iter;
typedef myMap::const_iterator mapiter;
iter it1 = dbgraph1->begin();
iter it2 = dbgraph2->begin();
int count =0;
myMap * newTable = NULL;
//iterating through the 2 samples of dbgraphs
while (it1 != dbgraph1->end() && it2 != dbgraph2->end())
{
//a match is found for 2 strings
if (it1->first == it2->first)
{
//the component ids of first sample
int compdb1 = it1->second->comp;
//the component ids of second sample
int compdb2 = it2->second->comp;
//inserting the component ids and counts in the map
newTable->insert(make_pair(make_pair(compdb1, compdb2), count));
count++;
for (mapiter it = newTable->begin(); it != newTable->end(); it++)
{
printf("%i %it %in", it->first.first, it->first.second, it->second);
}
it1++;
it2++;
}
//match not found
else
it1++;
it2++;
}
printf("nCLEARn");
return newTable;
}
这是错误:
Address 0x10 is not stack'd, malloc'd or (recently) free'd
Invalid read of size 8
Process terminating with default action of signal 11 (SIGSEGV)
Access not within mapped region at address 0x10
newTable
NULL
:
myMap * newTable = NULL;
并且从未在以下之前分配给有效对象:
newTable->insert(make_pair(make_pair(compdb1, compdb2), count));
取消引用NULL
指针是未定义的行为。为newTable
动态分配myMap
实例:
myMap* newTable = new myMap(); // Remember to delete later.
或使用堆栈分配的实例:
myMap newTable;
这是一个错误:
//match not found
else
it1++;
it2++;
导致it2
在循环的每次迭代中递增,并将导致在end()
迭代器上调用++
。更改为:
//match not found
else
{
it1++;
it2++;
}
或者为了简化代码,只需在循环中的一个位置递增迭代器,因为它们总是递增(在ifelse
的每个分支中)。
相关文章:
- 错误:无效的预处理指令 #i 的意思是 #if?
- is_same和variadic模板编译时错误无效转换
- printf 和 strftime 的参数错误无效
- 错误:无效使用非静态成员函数"int test::hotplug_callback(libusb_contex
- 我收到一个错误无效的操作数,类型为 const char [42] 和二进制"运算符+"的双倍数
- 错误:无效使用无效
- 发布信号量返回错误 6(无效句柄)
- C++17 可选树,错误:无效使用不完整的类型
- C++ 线程函数指针实现返回错误无效使用非静态成员函数
- 在测试中使用unique_ptr时指针错误无效
- 使用 malloc 而不是 new 会导致 free():指针错误无效
- valgrind错误无效读数4
- X11 XGetImage badmatch错误(无效的参数属性)
- free():C++中的指针错误无效
- 使用 valgrind 的地图中的读取大小错误无效
- 错误:无效的基类C
- 错误:无效使用了void表达式C++
- C++链接错误.无效
- free()错误:无效的下一个大小(fast)
- 我收到错误"无效使用不完整的类型'class map'