0
votes

In the following code, I am having a very hard time trying to figure out why I am getting a warning in Visual Studio 2019 about a buffer overflow.

Beneath, a function of a class is simply copying elements from one cstring array to another, but I am getting the error regardless of what I do.

The str[i] = mstr.str[i]; line is where all the issues are occuring. I am copying pointers.

NSString::NSString(const NSString& mstr)
{
    //Copy Constructor:  creates a string that is a duplicate of the argument
    //NEW STRING AS COPY

    //Create new string and copy data
    cap = mstr.cap;
    end = mstr.end;
    //createdCount = cap;
    //currentCount = end;
    str = new char[cap];

    for (int i = 0; i < mstr.end; ++i)
    {
        str[i] = mstr.str[i];
    }
}```

This is my code:
http://s000.tinyupload.com/index.php?file_id=13527548107981215670
Please include a minimal reproducible example in the question463035818_is_not_a_number
I'd say that in some situations end becomes greater than capAlexStepanov