好的,我将以下代码添加到游戏的mod菜单中,在Windows 7中一切正常。但是当我在Windows 8上将它发送给我的朋友时,他试图选择一个按钮(调用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继续
答案 0 :(得分:3)
您已经创建了一个长度为31的数组。因此您可以从索引0访问数组playerName到索引30.在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