我地图上的记忆问题在哪里?
Where is the memory issue in my map?
我正在从数据库加载数据并将其插入到地图中。当我尝试打印地图大小和数据时,它仍然显示大小为1,并且只打印最后一行。所有以前的数据将被覆盖并包含最后一个值。这里的任何指针问题。由于公司的问题,我更改了变量名。
我也做了一些检查点。所有键值都是唯一的
typdef long_char char[38];
for(int j = 0; j < 31; j++)
{
sample_enum param_sub_type = result_set[j];
long_char param_name;
strncpy(param_name,result_set[j], sizeof(param_name));
input_status_cd.insert(std::pair<long_char,sample_enum>(param_name, param_sub_type));
/*Insert Into Map */ <I suspect this may be issue but not sure>
}
/*Printing Size of map */
input_status_sd::size_type input_status_cd_size;
etlog_msg(intput_status_cd_size :] [%d]",intput_status_cd.size());
这是因为long_char
被定义为char[38]
,所以当您插入到映射中时,它将尝试使用operator<
,这将导致比较数组的第一个字符的地址。由于您是在循环中创建这个数组,因此很可能会分配相同的堆栈帧,因此每个对象的第一个字符的地址保持不变。因此,当map
认为您再次插入相同的对象时,所有先前的数据都会被覆盖。解决这个问题最简单的方法是使用std::string
作为long_char
的类型定义。
您应该使用std::map<std::string,sample_enum>
而不是std::map<long_char,sample_enum>
。这样,std::string
的operator<
将按预期工作(按字典顺序比较)。对于std::string
,你不能使用strncmp
来复制字符串,因为你必须使用赋值操作符。
相关文章:
- C++我需要了解在哪里使用指针和双指针
- 未定义的引用在哪里
- 谷歌测试中的期望值存储在哪里
- 尽管遵循了规则,内存泄漏在哪里
- 静态数据成员模板专用化的实例化点在哪里
- 在哪里放置我的函数?进入我的母语 Gui 还是进入我的演示者?
- 在哪里声明结构运算符重载
- C++ 中的自定义异常:在哪里定义它们?
- 常量参数存储在哪里 (C++)?
- C++泛型类错误,问题出在哪里?
- 我想知道变焦和轨道在哪里出了问题
- 线程清理器检测到数据争用,问题出在哪里?
- 我在Qt信号和插槽问题上的错误在哪里?
- 此(C )代码有什么问题吗?如果是这样,有人可以告诉我我在哪里出错
- 主要因素输出 - 问题出在哪里?
- 数据竞赛?段错误,但问题在哪里
- 在共享对象中命名不兼容的符号-在哪里查找问题
- 我地图上的记忆问题在哪里?
- 我没有获得目录中的实际文件数。问题出在哪里?
- c++这个问题的问题在哪里?(带参数的构造函数)