Microsoft C++ 异常: std::bad_alloc如何解决?

#程序报错

#include <iostream>
#include<string>
using namespace std;

struct Hero
{
    string hName;
    int age;
    string sex;
};

void bubbleSort(struct Hero heroArry[], int len)
{
    for (int i = 0; i < len-1; i++)
    {
        for (int j = 0; j < len - i - i; j++)
        {
            
            if (heroArry[j].age >heroArry[j + 1].age)
            {
                struct Hero temp = heroArry[j];
                heroArry[j] = heroArry[j + 1];
                heroArry[j + 1] = temp;
            }

        }
    }
}

void printHero(struct Hero heroArry[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << "Name: " << heroArry[i].hName 
            << " age:" << heroArry[i].age
            << " Sex:" << heroArry[i].sex << endl;
    }
}
int main() {

    struct Hero heroArry[5] =
    {
        {"Liu",23,"Man"},
        {"Guan",22,"Man"},
        {"Zhang",20,"Man"},
        {"Zhao",21,"Man"},
        {"Diao",19,"Women"},
    };

    int len = sizeof(heroArry) / sizeof(heroArry[0]);

    bubbleSort(heroArry, len);

    printHero(heroArry, len);

    system("pause");
    return 0;
}

img


网上说是内存不够了,但是没找到合适的解决办法

img


bubbleSort函数中for循环()中语句写错了
改正如下:

void bubbleSort(struct Hero heroArry[], int len)
{
    for (int i = 0; i < len-1; i++)
    {
        for (int j = 0; j < len - i - j; j++)
        {
            
            if (heroArry[j].age >heroArry[j + 1].age)
            {
                struct Hero temp = heroArry[j];
                heroArry[j] = heroArry[j + 1];
                heroArry[j + 1] = temp;
            }
 
        }
    }
}

如有帮助,还请帮忙点下采纳!感谢!

for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i - i; j++)//这里应该是j