使用 char* 变量查找不起作用

发布于 2024-09-16 11:08:05 字数 566 浏览 3 评论 0原文

我想知道为什么我有一个内存错误:

问题出现在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

︶ ̄淡然 2024-09-23 11:08:05

您需要将数组的长度加 1:

char* keync = new char[LEN+1];

您在分配的字符串之外以 null 终止。

(另外,你正在初始化aMap吗?)

You need to add 1 to the length of the array:

char* keync = new char[LEN+1];

You're null terminating outside the string you allocated.

(Also, are you initialising aMap?)

想念有你 2024-09-23 11:08:05

正如其他人指出的那样,您最好使用 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 a char* in to the map but when you are trying to find you are doing a new again. This ia a totally different pointer (although the string value they point is same) hence your lookup will fail.

海未深 2024-09-23 11:08:05

一个明显的问题是:

delete keync;

因为您使用了 new [],所以它应该是:

delete[] keync;

One obvious issue is the:

delete keync;

Because you used new [], it should be:

delete[] keync;
终陌 2024-09-23 11:08:05

您最好使用 std::string 而不是 char*。使用 std::string 的另一个优点是可以避免内存泄漏。

另一个解决方案是为map提供一个比较器函数,否则它们不会比较每个char*的内容,而是比较指向的地址。以下示例改编自 Sgi 的 std::map 文档:

struct comp
{
  bool operator()(char* s1, char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

map<char*, char*, comp> stringMap;

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:

struct comp
{
  bool operator()(char* s1, char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

map<char*, char*, comp> stringMap;
信仰 2024-09-23 11:08:05

好吧,对于初学者来说,您应该删除所有 char* 内容并使用 std::string,这可能会让您的问题消失。

仅供参考:
keync[LEN] = '\0'; // 这是错误的,将超出已分配数组的末尾

。 key 参数的复制是错误的。尝试一下大小:

using std::map;
map <char*, char*> aMap;

void search(const char* key) {
    const int LEN = strlen(key);

    char* keync = const_cast<char*>(key);

    char* value = aMap.find(keync)->second;

    printf("%s", value);
}

int main(int argc, char** argv)
{
    aMap["key0"] = "value0";
    search("key0");

    return 0;
}

您遇到的问题是因为您将 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 array

The copying of the key parameter is wrong. Try this on for size:

using std::map;
map <char*, char*> aMap;

void search(const char* key) {
    const int LEN = strlen(key);

    char* keync = const_cast<char*>(key);

    char* value = aMap.find(keync)->second;

    printf("%s", value);
}

int main(int argc, char** argv)
{
    aMap["key0"] = "value0";
    search("key0");

    return 0;
}

The problem you were having is because you were allocating keync as strlen(key) which doesn't count the null terminator. In your copy loop you were then overwriting the null terminator with the last char of key.

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).

这样的小城市 2024-09-23 11:08:05
using std::map;
map <char*, char*> aMap;

首先,该映射不会比较字符串(搜索内部),而是比较地址。所以基本上你不会通过输入字符串文字来找到任何 std::map::search 的东西。

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;

通过比较指针值(地址)执行搜索并不重要,因此返回的映射迭代器无效(它等于 aMap.end()),所以要么具有 null 或未分配的指针作为 第二 成员

    printf("%s", value);

    delete[] keync;
}

int _tmain(int argc, _TCHAR* argv[])
{
    a["key0"] = "value0";
    search("key0");

    return 0;
}

我希望它能解释为什么应该使用 std::string 而不是 char *

using std::map;
map <char*, char*> aMap;

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.

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];
    }

at this moment you have unterminated string, but it doesn't matter in your code

    char* value = aMap.find(keync)->second;

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 member

    printf("%s", value);

    delete[] keync;
}

int _tmain(int argc, _TCHAR* argv[])
{
    a["key0"] = "value0";
    search("key0");

    return 0;
}

I hope it explains you why should you use std::string instead of char *

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文