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

时间:2015-04-14 17:46:40

标签: c++ struct malloc

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

我认为如果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);
     }


}

printtruct()代码:

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;
}

2 个答案:

答案 0 :(得分:2)

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

使用智能指针。

使用智能指针,你没有(通常)关心内存管理。

兴趣链接:

Smart Pointers (Modern C++)Smart pointer

始终可以使用std::string

您使用C ++语言进行编码,请不要使用它?你绑定使用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]);
    }
}

答案 1 :(得分:1)

当我复制/粘贴你的代码时,由于以下原因我得到了一个段错误:

 log.arr->players = (Player*)malloc(7 * sizeof(Player*));

您忘记添加索引,因此每次进行循环时它都会尝试在同一位置使用malloc内存,从而导致段错误。同样如评论中所述,您应该使用sizeof(播放器)而不是sizeof(播放器*)。

遵循你的风格,它应该是:

 (log.arr + i)->players = (Player*)malloc(7 * sizeof(Player));

我建议将Team * arr作为std :: vector,将Player *播放器作为std :: vector(假设您需要一个动态容器)。 Vector将负责内存管理(通过其Allocator),并将提高代码的可读性,并使其在未来更容易使用(代码重用)。