简体   繁体   English

用char *变量查找不起作用

[英]find with char* variable doesnt work

I'd like to know why I have a memory error with this: 我想知道为什么我有一个内存错误:

The problem appears on char* value = aMap.find(keync)->second 问题出现在char * value = aMap.find(keync)-> second

If I put manualy char* value = "key0" it works!!! 如果我手动输入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;
}

You need to add 1 to the length of the array: 您需要在数组长度上加1:

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

You're null terminating outside the string you allocated. 您在分配的字符串之外终止为null。

(Also, are you initialising aMap?) (此外,您正在初始化aMap吗?)

As others pointed, you are much better off using std::string for this. 正如其他人指出的那样,您最好使用std::string Now, for the actual problem why you are not able to find the string is because you are storing pointers in the map ie 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. 您在地图中插入了一个char* ,但是当您尝试查找时,您又在做new This ia a totally different pointer (although the string value they point is same) hence your lookup will fail. 这是一个完全不同的指针(尽管它们指向的字符串值相同),因此您的查找将失败。

One obvious issue is the: 一个明显的问题是:

delete keync;

Because you used new [], it should be: 因为您使用了new [],所以它应该是:

delete[] keync;

You are better using std::string instead of char*. 您最好使用std :: string代替char *。 Another plus of using std::string is that you will avoid memory leaks. 使用std :: string的另一个好处是可以避免内存泄漏。

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. 另一个解决方案是为map提供比较器功能,否则它们将不比较每个char *的内容,而是比较所指向的地址。 The following example was adapted from Sgi's std::map documentation: 以下示例改编自Sgi的std :: map文档:

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

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

Well look for starters you should drop all the char* stuff and use std::string , this would probably make your problem go away. 好吧,对于初学者,您应该删除所有char *内容并使用std::string ,这可能会使您的问题消失。

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. 您遇到的问题是因为您将keync分配为strlen(key)而不计算空终止符。 In your copy loop you were then overwriting the null terminator with the last char of key . 然后,在复制循环中,您将用key的最后一个字符覆盖null终止符。

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). 复制输入字符串的整个想法是错误的,我在我的解决方案中将其替换为const,因为这样做更有意义(就目前而言)。

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. 因此,基本上,通过键入字符串文字,您将不会在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];
    }

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 在这里,您通过比较指针值(地址)来执行搜索,因此返回的地图迭代器无效(等于aMap.end()),因此它具有null或未分配的指针作为second成员

    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 * 我希望它能解释您为什么应该使用std :: string而不是char *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM