简体   繁体   English

为什么我的应用程序崩溃了? 我的代码有什么问题?

[英]Why my application is crashing? What's wrong with my code?

char* szWords[] = { "caralho", "porra" };
if (IsGoldGrade(pObj)) //Ignore, its ok. //Crashing after i added the
{
    for (int x = 0; x < sizeof(szWords); x++) {
        if (strstr((strlwr((char*)szWords[x])), szChat)) {
            char szBuffer[256]; 
            sprintf(szBuffer, "You can't type %s", szWords[x]);
            Announce(pObj, szBuffer);
            memset(szBuffer, 0, 256);
            return;
        }
    }
}

Idk but I can't use this as "code" on stackoverflow. IDK,但我不能在stackoverflow上将其用作“代码”。

Pastebin: http://pastebin.com/u8yit8Rw 粘贴框: http//pastebin.com/u8yit8Rw

PS: I can't use StrStrI because im using Visual Studio 2003. PS:我不能使用StrStrI,因为我正在使用Visual Studio 2003。

Your for loop condition is wrong. 您的for循环条件错误。 You want to iterate the array of pointers to char. 您想迭代指向char的指针数组。
Your loop for (int x = 0; x < sizeof(szWords); x++) continues while x < sizeof(szWords) . for (int x = 0; x < sizeof(szWords); x++)继续进行,而x < sizeof(szWords)继续。 But sizeof(szWords) is not array length. 但是sizeof(szWords) 不是数组长度。 It just says how many bytes your array occupies in memory. 它只是说您的阵列在内存中占用多少字节。 It is system dependant, however it is twice the size of pointer to char, so probably 8 or 16 bytes. 它取决于系统,但是它是char指针大小的两倍,因此可能是8或16个字节。 You need to divide this size by size of the array element then you will get the proper array size. 您需要将此大小除以数组元素的大小,然后将获得正确的数组大小。

Rewrite your for loop like this: 像这样重写您的for循环:

for (int x = 0; x < sizeof(szWords)/sizeof(szWords[0]); x++)

or if your compiler supports C++11 you can try range-based for: 或者如果您的编译器支持C ++ 11,则可以尝试基于范围的用于:

for (const char *word : szWords)

Apart from that, if you are writing C++ code you really should use STL and other C++ features. 除此之外,如果您正在编写C ++代码,则确实应该使用STL和其他C ++功能。 For instance your array of strings should be declared as: 例如,您的字符串数组应声明为:

std::vector<std::string> words = { "caralho", "porra" };

or if your compiler doesnt support C++11 (then really change it...) 或如果您的编译器不支持C ++ 11(然后进行更改...)

std::vector<std::string> words;
words.push_back("caralho");
words.push_back("porra");

for (std::size_t i = 0; i < words.size(); ++i) {
    // since you are using C string functions you will need word as C string
    const char *word = words[i].c_str();
    // do whatever you want with word
}

Also consider reading modern C++ book before writing code. 在编写代码之前,也请考虑阅读现代C ++书籍。

From the look of it, this is a function which checks if the user has written a prohibited word? 从外观上看,这是一个检查用户是否写了禁止词的功能?

I'd replace char* szWords[]... with std::vector<std::string> to store the prohibited words, and use std::find to see if the input is in that list. 我用std::vector<std::string>替换char* szWords[]...来存储禁止的单词,并使用std::find来查看输入内容是否在该列表中。

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> bannedWords{"hamster", "elderberries", "etcetera"};

bool isBanned(const std::string &str) {
  return std::find(bannedWords.begin(), bannedWords.end(), str) != bannedWords.end();
}

int main() {
  std::cout << "Is 'wally' banned? " << isBanned("wally") << std::endl;
  std::cout << "Is 'elderberries' banned? " << isBanned("elderberries") << std::endl;
}

More information about std::find is here . 有关std::find更多信息在这里

Here's an online demo 这是一个在线演示

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

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