c++ - 为什么我的 C++ 代码在 Windows 7 上运行良好但在 Windows 8 上崩溃?

标签 c++ windows-8 windows-7

好的,我将以下代码添加到游戏的模组菜单中,并且在 Windows 7 中对我来说一切正常。但是当我在 Windows 8 上将它发送给我的 friend 时,他试图选择一个按钮(调用 GetClients () 函数),游戏就崩溃了。知道为什么吗?

char* playerNames[31] = {};
int getUID(char* pName)
{
int i = 0;
while (i < 31) {
    char* pNamesec = (char*)PLAYER::GET_PLAYER_NAME((Player)(i));
    if (pNamesec == pName) {
        return i;
    }
    //else { break; }
    i++;
}
}
char* getPnameAt(int id) {
for (int i = 0; i < 30; i++) {
    if (i == id) {
        return (char*)PLAYER::GET_PLAYER_NAME((Player)(i));

    }
}
}
void GetClients()
{
playerNames[31] = {};
int i = 0;
while (i < 100) {
    char* pName = (char*)PLAYER::GET_PLAYER_NAME((Player)(i));
    if (wcslen((WCHAR*)pName) > 3) {
        if (getUID(pName) == i) {
            playerNames[i] = pName;
        } else {
            getPnameAt(i);
        }
    }
    i++;
}
i = 0;
}

弹出的错误消息说: CORE: 执行modmenu.asi时出现异常,按ok继续

最佳答案

您已经创建了一个长度为 31 的数组。因此您可以从索引 0 到索引 30 访问数组 playerName。在 GetClients() 中

playerNames[31] = {}; //Observe this line
while (i < 100) {
  // Indexes greater than 30 are being used to access playerNames array
}

31 或以上不是 playerNames 数组的有效索引,您会遇到未定义的行为。

所以如果你想在运行时添加playerNames。以下是可能对您有所帮助的小示例..

int main()
{
     vector<string> playerNames;
     playerNames.push_back("XYZ");
     playerNames.push_back("ABC");
     // To access from vector
     vector<string>::iterator itr = vec.begin();
     for(;itr!=vec.end();itr++)
     {
        cout<<*itr<<endl;
     }
}

阅读更多 here

关于c++ - 为什么我的 C++ 代码在 Windows 7 上运行良好但在 Windows 8 上崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30389707/

相关文章:

c++ - 适用于 C++ (Linux) 的 VisualWorks Smalltalk 类 IDE

c++ - 如何检查空行?

c++ - 数组大小取决于 sizeof() 结构字段

java - Selenium 网络驱动程序 : Able to open Firefox browser but unable to load Url

android - C++ Opencv 重新缩放边界矩形

windows-8 - 自定义 ItemTemplate 未显示在 Visual Studio 2010 中 - 添加新项

windows-8 - 禁用 ListView 中特定项目的选择和 itemInvoke 事件

javascript - Visual Studio 2013-Javascript

delphi - 需要在windows中解析HMONITOR --> deviceName(或deviceName --> HMONITOR)

WCF 适用于 XP 但不适用于 Windows 7