gpt4 book ai didi

c++ - 打印结构的结构数组会导致意外的执行错误?

转载 作者:行者123 更新时间:2023-11-28 00:16:54 25 4
gpt4 key购买 nike

对于这个结构定义,我初始化了它们,然后用 printstruct() 打印它们的内容;但是在打印第一个元素后程序意外挂起,

1.我在想如果 malloc() 失败它会为所有元素正确吗?
2.不幸的是,我必须使用这些结构,下面的代码有什么问题吗?

注意:所有 char 数组的大小等于它们将接收的内容的最大长度加上一个空终止字符“\0”,每个团队最多有 7 名玩家,团队数量为 21000。
很抱歉一种错误检查问题,但我过去从未使用过 C++ 中的结构。

typedef struct Player{
char firstName[50];
char lastName[50];
}Player;

typedef struct Team{
int id;
char title[50];
char summary[100];
int numberOfPlayers;
Player *players;
}Team;

typedef struct TeamsLog{
Team *arr;
int numberOfTeams;
}Teams;

int main()
{
TeamsLog log;
log.numberOfTeams = 21000;
log.arr = (Team*)malloc(log.numberOfTeams * sizeof(Team));

for (int i = 0; i < log.numberOfTeams; ++i)
{
(log.arr+ i)->id = 1;
(log.arr + i)->numberOfPlayers = 7;
for (int l = 0; l < 10; ++l)
{
(log.arr + i)->summary[l] = '0' + l;
(log.arr + i)->title[l] = '0' + l;
}
(log.arr + i)->summary[10] = '\0';
(log.arr + i)->title[10] = '\0';
log.arr->players = (Player*)malloc(7 * sizeof(Player*));
for (int l = 0; l < 7; ++l)
{
for (int k = 0; k < 10; ++k){
(log.arr + i)->players[l].firstName[k] = '0' + k;
(log.arr + i)->players[l].lastName[k] = '0' + k;
}
(log.arr + i)->players[l].firstName[10] = '\0';
(log.arr + i)->players[l].lastName[10] = '\0';
}
printstruct(log.arr + i);
}


}

printstruct()的代码:

void printstruct(Team* arg)
{
cout << "\nTeam Name: " << arg->title
<< "\nId: " << arg->id
<< "\nSummary: " << arg->summary
<< "\nNumber of players: " << arg->numberOfPlayers << endl;
for (int j = 0; j < arg->numberOfPlayers; ++j)
cout << "Player " << j+1 << " " << ((arg->players) + j)->firstName << " "
<< ((arg->players) + j)->lastName << endl;
}

最佳答案

由于您使用的是 C++,我想向您提出建议,我要写的内容太长,无法在评论中使用,因此被写为答案。

使用智能指针。

使用智能指针,您(通常)不必关心内存管理。

兴趣链接:

Smart Pointers (Modern C++) , Smart pointer

使用 std::string你总是可以的。

您正在使用 C++ 语言进行编码,怎么不使用它?您是否绑定(bind)使用 char

同一个程序(C++):

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

typedef struct Player{
std::string firstName;
std::string lastName;
}Player;

typedef struct Team{
int id;
std::string title;
std::string summary;
int numberOfPlayers;
std::vector<Player> players;
}Team;

typedef struct TeamsLog{
std::vector<Team> arr;
int numberOfTeams;
}Teams;


void printstruct(const Team& team)
{
std::cout << "\nTeam Name: " << team.title
<< "\nId: " << team.id
<< "\nSummary: " << team.summary
<< "\nNumber of players: " << team.numberOfPlayers << std::endl;


int index = 0;
for (auto &player : team.players)
{
std::cout << "Player " << ++index << " " << player.firstName << " " << player.lastName << std::endl;
}
}

int main()
{
TeamsLog log;
log.numberOfTeams = 21000;
log.arr.reserve(21000); // This is not really needed but improve performance.

for (int i = 0; i < log.numberOfTeams; ++i)
{
Team team;
team.id = 1;
team.numberOfPlayers = 7;

team.summary = "some summary";
team.title = "some title";

team.players.reserve(7);

for (int l = 0; l < 7; ++l)
{
Player player;
player.firstName = "Some name";
player.lastName = "Some last name";
team.players.push_back(player);
}
log.arr.push_back(team);
printstruct(log.arr[i]);
}
}

关于c++ - 打印结构的结构数组会导致意外的执行错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29634078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com