"New" 操作员没有分配足够的内存

"New" operator not allocating enough memory

我在学校学习更多,但我想挑战一下自己。我们最近进入了结构,只是勉强并且从中获得乐趣。

我尝试制作一个立直麻将生成器,以生成一组用于墙壁的瓷砖。现在只有这一代,但目前我无法用所需的全部 136 个图块填充数组。

我为 tile 本身和 tileSet 创建了 headers。瓦片 header 处理单个瓦片,它由类型和等级分开。 Tile-set 构建了整个 "deck" 或他们所说的 "wall"。整个事情会一直进行到荣誉瓷砖开始进入,一旦它达到索引 115,它就会崩溃 "bad alloc"

我知道使用命名空间标准;是一件坏事,我更喜欢这种方式,因为我现在正在自己做这件事。我有点厌倦了几乎所有的东西都必须写 std:: 。现在我没有使用任何其他库。

我也是基于 "deck of cards" 结构的结构。

主文件

#include <iostream>
#include <string>
#include "tileSet.h"

using namespace std;


int main()
{
    tileSet tileWall;
    tile currentTile;

    tileWall.printSet();
    tileWall.shuffle();

    cout << endl << endl;

    tileWall.printSet();
    tileWall.shuffle();

    int count = 0;
    for (int i = 0; i < (136 - 28); i++)
    {
        currentTile = tileWall.dealTile();
        cout << currentTile.print() << endl;
        count++;
    }
    cout << endl;
    cout << count << endl;

    system("pause");
    return 0;
}

tile.h

#ifndef H_tile
#define H_tile
#include <string>
#include <iostream>

using namespace std;

class tile
{
public:
    tile(string tileType, string tileRank);
    string print() const;
    tile();
private:
    string type;
    string rank;
};

tile::tile()
{

}

tile::tile(string tileType, string tileRank)
{
    type = tileType;
    rank = tileRank;
}

string tile::print() const
{
    return (rank + " of " + type);
}
#endif

tileSet.h

#ifndef H_tileSet
#define H_tileSet
#include "tile.h"
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <stdlib.h> 


using namespace std;
int const numTiles = 136;

class tileSet
{
public:
    tileSet();
    void shuffle();
    tile dealTile();
    void printSet() const;
private:
    tile *tileWall;
    int currentTile;
    int index;
};

void tileSet::printSet() const
{
    cout << left;
    for (int i = 0; i < numTiles; i++)
    {
        cout << setw(19) << tileWall[i].print();
    }
}

tileSet::tileSet()
{
    string type[] = { "Pin", "Sou", "Wan", "Honor" };
    string rank[] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "East", "South", "West", "North", "Haku", "Hatsu", "Chun" };

    tileWall = new tile[numTiles];


    currentTile = 0;
    index = 0;
    //Populate with Pin tiles
    for (int i = 0; i < 36; i++)
    {
        tileWall[index++] = tile(type[0], rank[i % 9]);
    }
    //Populate with Sou tiles
    for (int i = 0; i < 36; i++)
    {
        tileWall[index++] = tile(type[1], rank[i % 9]);
    }
    //Populate with Wan tiles
    for (int i = 0; i < 36; i++)
    {
        tileWall[index++] = tile(type[2], rank[i % 9]);
    }
    //Populate with Honor tiles
    for (int i = 0; i < 28; i++)
    {
        tileWall[index++] = tile(type[3], rank[i % 16 + 9]);
    }
}

void tileSet::shuffle()
{
    currentTile = 0;
    for (int first = 0; first < numTiles; first++)
    {
        int second = (rand() + time(0)) % numTiles;
        tile temp = tileWall[first];
        tileWall[first] = tileWall[second];
        tileWall[second] = temp;
    }
}

tile tileSet::dealTile()
{
    if (currentTile > numTiles)
        shuffle();
    if (currentTile < numTiles)
        return (tileWall[currentTile++]);
    return (tileWall[0]);
}
#endif

rank[i % 16 + 9] 是未定义的行为,例如对于i=7,因为7%16+9 = 16,但是rank的大小只有16.