我如何在 VC++ 中定义一个 bool 数组,其中包含超过 MAXINT 元素

How can i define a bool array in VC++ with more than MAXINT Elements

在 C++ 中定义

unsigned long long myDimension
bool myVar[myDimension];

是可以的,当 myDimension 大于 0xFFFFFFFF 时也是可以的,但是访问索引大于 0xFFFFFFFF 的元素将导致运行时出错。

这是我的错误消息:Ausnahmefehler bei 0x00007FF7CACD01DC in Multiply.exe: Vom RangeChecks-Instrumentationscode wurde ein Arrayzugriff außerhalb des Bereichs erkannt。

所以我的问题是如何编写一个自己的 class,它的行为类似于普通数组并且可以处理更多元素。

容易出错的代码:

bool vergleichsMintherme[0x1000000000]; 
main()
{
    vergleichsMintherme[0x100000000] = false;
}
class largeBoolArray
{
private:
    bool **myMintherms;
    unsigned long long dim;
    unsigned int dimPointers;
public:
    largeBoolArray()
    {
        myMintherms = NULL;
        dim = 0;
        dimPointers = 0;
    };
    largeBoolArray(unsigned long long Dim)
    {
        assert(Dim > 0);
        dim = Dim;
        dimPointers = ((unsigned int) (Dim & 0xFFFFFFFF80000000) >> 31) + 1;
        myMintherms = new bool*[dimPointers];
        for (unsigned int i = 0; i < dimPointers; i++)
        {
            while (dim > 0)
            {
                if (dim > 0x80000000)
                {
                    myMintherms[i] = new bool[0x80000000];
                    for (unsigned long long j = 0ll; j < 0x7fffffff; j++)
                        myMintherms[i][j] = false;
                    dim -= 0x80000000;
                }
                else
                {
                    myMintherms[i] = new bool[dim];
                    for (unsigned long long j = 0ll; j < dim; j++)
                        myMintherms[i][j] = false;
                    dim = 0;
                }
            }
            dim = Dim;
        }
    }
    ~largeBoolArray()
    {
        if (myMintherms != NULL)
        {
            for (unsigned int i = 0; i < dimPointers; i++)
            {
                if (myMintherms[i] != NULL)
                delete[] myMintherms[i];
            }
            delete[] myMintherms;
        }
    }
    bool& operator[] (unsigned long long selektor)
    {
        unsigned int firstIndex = (unsigned int)(selektor & 0xFFFFFFFF80000000) >> 31;
        unsigned int secondIndex = (selektor & 0x7FFFFFFF);
        return myMintherms[firstIndex][secondIndex];
    }
    largeBoolArray operator= (largeBoolArray o1)
    {
        this->dim = o1.dim;
        this->dimPointers = o1.dimPointers;
        this->myMintherms = new bool*[dimPointers];
        unsigned long long dimRest;
        for (unsigned int i = 0; i < dimPointers; i++)
        {
            dimRest = dim;
            while (dimRest > 0)
            {
                if (dimRest > 0x80000000)
                {
                    this->myMintherms[i] = new bool[0x80000000];
                    for (unsigned long long j = 0ll; j < 0x7fffffff; j++)
                        this->myMintherms[i][j] = o1.myMintherms[i][j];
                    dimRest -= 0x80000000;
                }
                else
                {
                    this->myMintherms[i] = new bool[dimRest];
                    for (unsigned long long j = 0ll; j < dimRest; j++)
                        this->myMintherms[i][j] = o1.myMintherms[i][j];
                    dimRest = 0;
                }
            }
        }

        return *this;
    }
    void clear()
    {
        for (unsigned long long i = 0ll; i < dim; i++)
            (*this)[i] = false;
    }
};
  1. 你必须在 64 位模式下编译,在 32 位模式下你的程序没有地址 space 来适应这样的数组

  2. 您必须在堆上分配数组,如果将其设为全局数组,可执行文件将太大,如果在堆栈上分配,则会溢出堆栈

  3. 你需要小心并且只在 size_t 变量而不是 int

  4. 中传递数组索引值

除此之外,C++ 还支持访问大内存块。

这是有效的旧 C 风格数组分配(在 VC2017 上测试):

 int* reallyLargeArray = (int*)malloc(sizeof(int)*0x100000002);
 reallyLargeArray[0x100000001] = 5;

std::vector 也有效

  std::vector<int> reallyLargeArray;
  reallyLargeArray.resize(0x100000002);
  reallyLargeArray[0x100000001] = 5;

C++ 样式分配 (new int[0x100000002]) 不起作用,