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

-5

In C++ the definition

unsigned long long myDimension
bool myVar[myDimension];

is possible, also when myDimension is bigger than 0xFFFFFFFF, but the access of an element at index bigger than 0xFFFFFFFF will cause an error while runtime.

This was my error message: Ausnahmefehler bei 0x00007FF7CACD01DC in Multiply.exe: Vom RangeChecks-Instrumentationscode wurde ein Arrayzugriff außerhalb des Bereichs erkannt.

So my Question is how to write a own class which behaves like a normal array and can handle more elements.

Error prone code:

bool vergleichsMintherme[0x1000000000]; 
main()
{
    vergleichsMintherme[0x100000000] = false;
}
c++
arrays
elements
asked on Stack Overflow Jul 29, 2018 by SchwertAs • edited Aug 1, 2018 by SchwertAs

2 Answers

3
  1. you must compile in 64bit mode, in 32bit your program just doesn't have the address space to fit such an array

  2. you must allocate the array on the heap, if you make it a global array the executable will be too large and if you allocate on the stack you will overflow the stack

  3. You need to be careful and only pass array index values in size_t variables not int

other then that C++ supports access to large memory blocks just fine.

Here is old-C-style array allocation that works (tested on VC2017):

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

std::vector also works

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

C++ style allocation (new int[0x100000002]) does not work,

answered on Stack Overflow Jul 29, 2018 by Nir • edited Jul 29, 2018 by Nir
-1
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;
    }
};
answered on Stack Overflow Jul 29, 2018 by SchwertAs • edited Aug 1, 2018 by SchwertAs

User contributions licensed under CC BY-SA 3.0