使用 char* 变量查找不起作用
我想知道为什么我有一个内存错误:
问题出现在 char* value = aMap.find(keync)->second
如果我手动输入 char* value = "key0" 它可以工作!
using std::map;
map <char*, char*> aMap;
void search(const char* key) {
const int LEN = strlen(key);
char* keync = new char[LEN];
for (int i= 0; i < LEN; i++) {
keync[i] = key[i];
}
char* value = aMap.find(keync)->second;
printf("%s", value);
delete[] keync;
}
int _tmain(int argc, _TCHAR* argv[])
{
a["key0"] = "value0";
search("key0");
return 0;
}
I'd like to know why I have a memory error with this:
The problem appears on char* value = aMap.find(keync)->second
If I put manualy char* value = "key0" it works!!!
using std::map;
map <char*, char*> aMap;
void search(const char* key) {
const int LEN = strlen(key);
char* keync = new char[LEN];
for (int i= 0; i < LEN; i++) {
keync[i] = key[i];
}
char* value = aMap.find(keync)->second;
printf("%s", value);
delete[] keync;
}
int _tmain(int argc, _TCHAR* argv[])
{
a["key0"] = "value0";
search("key0");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要将数组的长度加 1:
您在分配的字符串之外以 null 终止。
(另外,你正在初始化aMap吗?)
You need to add 1 to the length of the array:
You're null terminating outside the string you allocated.
(Also, are you initialising aMap?)
正如其他人指出的那样,您最好使用 std::string 来实现此目的。现在,对于实际问题,为什么您无法找到字符串是因为您在映射中存储了指针,即映射的键是指针变量。您在地图中插入了一个
char*
,但是当您尝试查找时,您又在执行一个new
。这是一个完全不同的指针(尽管它们指向的字符串值相同),因此您的查找将失败。As others pointed, you are much better off using
std::string
for this. Now, for the actual problem why you are not able to find the string is because you are storing pointers in the map i.e. the key to the map is a pointer variable. You inserted achar*
in to the map but when you are trying to find you are doing anew
again. This ia a totally different pointer (although the string value they point is same) hence your lookup will fail.一个明显的问题是:
因为您使用了 new [],所以它应该是:
One obvious issue is the:
Because you used new [], it should be:
您最好使用 std::string 而不是 char*。使用 std::string 的另一个优点是可以避免内存泄漏。
另一个解决方案是为map提供一个比较器函数,否则它们不会比较每个char*的内容,而是比较指向的地址。以下示例改编自 Sgi 的 std::map 文档:
You are better using std::string instead of char*. Another plus of using std::string is that you will avoid memory leaks.
The other solution will be to provide map with a comparator function, otherwise they will not compare the content of each char*, but instead the address pointed. The following example was adapted from Sgi's std::map documentation:
好吧,对于初学者来说,您应该删除所有 char* 内容并使用
std::string
,这可能会让您的问题消失。仅供参考:
keync[LEN] = '\0';
// 这是错误的,将超出已分配数组的末尾。 key 参数的复制是错误的。尝试一下大小:
您遇到的问题是因为您将
keync
分配为strlen(key)
,它不计算空终止符。在复制循环中,您使用key
的最后一个字符覆盖空终止符。复制输入字符串的整个想法是错误的,我在解决方案中将其替换为 const 强制转换,因为它更有意义(就目前而言)。
Well look for starters you should drop all the char* stuff and use
std::string
, this would probably make your problem go away.FYI:
keync[LEN] = '\0';
// this is wrong and will be one past the end of the allocated arrayThe copying of the key parameter is wrong. Try this on for size:
The problem you were having is because you were allocating
keync
asstrlen(key)
which doesn't count the null terminator. In your copy loop you were then overwriting the null terminator with the last char ofkey
.The whole idea of copying the input string is wrong and I have replaced it with a const cast in my solution as it makes more sense (as far as that goes).
首先,该映射不会比较字符串(搜索内部),而是比较地址。所以基本上你不会通过输入字符串文字来找到任何 std::map::search 的东西。
此时您有未终止的字符串,但在您的代码中
通过比较指针值(地址)执行搜索并不重要,因此返回的映射迭代器无效(它等于 aMap.end()),所以要么具有 null 或未分配的指针作为
第二
成员我希望它能解释为什么应该使用 std::string 而不是 char *
first of all, this map won't compare strings (inside of search) but addresses. So basicly you won't find anything with std::map::search by typing string literals.
at this moment you have unterminated string, but it doesn't matter in your code
here you're performing search by comparing pointers values (addresses), so returned map iterator is invalid (it's equal aMap.end()), so either it has null or unallocated pointer as
second
memberI hope it explains you why should you use std::string instead of char *