Linux与Windows的c++差异:std::map
C++ Differences between Linux and Windows with std::map
我试图使用std::map来保存配置细节,但是我在Windows (Windows 8 64位)和Linux (Ubuntu 12.04 64位)之间得到不同的行为
下面是代码片段:std::map<std::string, std::string> testMap;
testMap.insert(std::pair<std::string, std::string>("test1", "value1"));
testMap.insert(std::pair<std::string, std::string>("test2", "value2"));
std::cout << testMap["test1"] << std::endl;
在windows上,这将返回预期的"value1",但是在Linux上,这会导致Seg Fault。
谁能给我指个正确的方向?上面的代码很好,在Linux机器上可以正常工作。您已经提到,在访问该行时,您正在获得分段错误,这表明您的程序的其他逻辑正在进行某种内存损坏。
std::cout << testMap["test1"] << std::endl;
堆损坏通常是在进程中加载的一些DLL/模块已经发生真正的损坏之后检测到的,当前逻辑(程序崩溃的地方)可能是受害者。然而,由于在Linux机器上观察到这个问题,您可能希望使用Valgrind。这是你应该附加你的程序(a.out)的方式。
$ valgrind --tool=memcheck --db-attach=yes ./a.out
当程序检测到内存错误时,Valgrind将在调试器中附加程序(自动),以便您可以进行实时调试(GDB)。这样你就能找到问题的根本原因。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 使用一个考虑到std::map中键值的滚动或换行的键
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- C++如何创建 std::map
- 从其他容器中移动构造"std::map"
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射
- 使用重载 [] 运算符返回 std::map() 的可赋值
- std::map, std::unordered_map - 缩小初始值设定项列表中的转换范围
- C++ 使用枚举类对象分配 std::map 值
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- std::map:当元素不可默认构造时创建/替换元素
- Arduino编译器和STL:使用std::vector和std::map