【发布时间】:2010-11-23 01:44:00
【问题描述】:
我到了让自己感到困惑的地步,但这就是我所拥有的。我最近才开始熟悉指针,直到我觉得使用它们更舒服,但我收到一个错误,提示 strcpy_s() 中的缓冲区太小。
请不要对我使用 char 数组而不是 std::string 来介绍我,它用于 HL2SDK,它以 char 数组为中心(不知道为什么),所以我只是坚持这个模式。
void func_a()
{
char *szUserID = new char[64];
char *szInviterID = new char[64];
char *szGroupID = new char[64];
sprintf(szUserID, "%I64d", GetCommunityID(szUserSteamID));
sprintf(szInviterID, "%I64d", GetCommunityID(g_CvarSteamID.GetString()));
GetGroupCommunityID(1254745, &szGroupID); // Group Steam Community ID
}
void GetGroupCommunityID(int groupID, char **communityID)
{
int staticID = 1035827914;
int newGroupID = 29521408 + groupID;
char *buffer = new char[64];
snprintf(buffer, sizeof(buffer), "%d%d", staticID, newGroupID);
strcpy_s(*communityID, sizeof(*communityID), buffer);
delete buffer;
}
【问题讨论】:
-
如果您需要动态分配的
char数组,通常可以使用std::vector<char>。我不明白为什么这在这里行不通。 -
为什么要新建和删除刚刚在堆栈上声明就可以完美完成的字符缓冲区?