seemingly random access violation error on creating dynamic array

-2

I'm trying to write a class that stores an array of doubles, but I'm having problems with a seemingly inconsistent error that will sometimes trigger if an array is initialized with too large a number

The relevant code is here:

int Size;
double *Dimentions;

VectorN::VectorN(const VectorN &Old)
{
    //Would have the same functionallity as the assignment operator, so just use that
    operator=(Old);
}

void VectorN::operator=(const VectorN &rhs)
{
    //Set the size and intialise the array
    Size = rhs.Size;

    //error occurs here
    Dimentions = new double[rhs.Size];
}

The specific error is "0xC0000005: Access violation reading location 0xFFFFFFF8"

The reason that I call it "random" is because it only seems to occur when rhs.Size is any value larger than ~90, and replacing rhs.Size with a constant such as 92 will only sometimes trigger the error, whereas sometimes the program will sometimes run without issue, despite nothing changing.

I would normally assume that this was somehow related to the computer not having enough memory, however this has persisted throughout multiple restarts when there should be ample free memory.

I've also tried creating and deleting local arrays within the function before the initialization of Dimensions and they seem to cause the same error if they are large enough.

I am using Visual Studio 2017, if that's relevant.

c++
asked on Stack Overflow Dec 6, 2018 by Gliss • edited Dec 6, 2018 by Gliss

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0