0

The below program creates a new array of structs of [n+5] elements, every time the quantity reaches 5, 10, 15, ... and copies the old elements to a new array.

This is the error thrown:

Exception thrown at 0x7AE740DF (vcruntime140d.dll) in ConsoleApplication4.exe: 0xC0000005: Access violation writing location 0x6CBFEBB8.

The error is thrown on line 69, where it says items += 1;

#include <iostream>
#include <iomanip>

using namespace std;

struct item{
    string name;
    int eff;
};

item* ptr = nullptr;
item* temp = nullptr;
int itemsA = 0;
int arrSize = 5;
int lastElement = 0;


void func(int&, item *& ,int&, int& , item*&);

int main()
{
    ptr = new item[arrSize];

    func(arrSize, ptr,itemsA,lastElement, temp);


}
void func(int& arrSize, item *& ptr, int &items, int&lastElement, item *& temp)
{
    bool event = false;
    int a = 1;
    while (a == 1)
    {
        cout << "Your array size is: " << arrSize << endl;
        if (items > arrSize)
        {
            temp = new item[arrSize];

            for (int x = 0; x < arrSize; x++)
            {
                temp[x].eff = ptr[x].eff;
                temp[x].name = ptr[x].name;
            }
            arrSize += 5;
            delete[] ptr;
            ptr = nullptr;
            ptr = new item[arrSize];

            for (int x = 0; x < arrSize; x++)
            {
                ptr[x].name = temp[x].name;
                ptr[x].eff = temp[x].eff;
            }
            delete[] temp;
            temp = nullptr;

        }
        int any;
        string any2;
        cout << "ENter name for position number: " << lastElement + 1 << "\n";
        cin >> any2;
        cout << "ENter int for hp effect for position number: " << lastElement + 1 << "\n";
        cin >> any;
        ptr[lastElement].eff = any;
        ptr[lastElement].name = any2;
        items +=1;                                //////////the error is thrown here
        cout << "You now have " << items << " items.\n";


        cout << "Items in your bag: \n";
        for (int x = 0; x < items; x++)
        {
            cout << ptr[x].name << " which gives you extra " << ptr[x].eff << " health when used.\n";
        }
        lastElement += 1;

    }
}
5
  • The second for loop in func() accesses elements of temp past the end. You subsequent code does not check that lastElement is a valid index for ptr, but uses it as an index. Accessing an invalid index of an array (dynamically allocated or not) does not magically resize that array - it simply causes undefined behaviour. Commented Mar 19, 2020 at 2:26
  • Unrelated: To be able to analyse programming problems, try to make every little part of the program clear. A function should have one function/purpose. If the descriptive name of a function would need to be longer than the code performing the function, split the function up in parts. If you come up with a name for a function like a_function_that_first_does_this_and_then_this_but_only_if_that it's a sure sign that you should split it. Your main (proxied via a call to a void func(int&, item *& ,int&, int& , item*&);) looks like it has some of that. Commented Mar 19, 2020 at 3:26
  • @Peter, thank you for your tip regarding the for loop. As soon as I fixed this problem the code worked as expected. As for the int lastElement being used as an index, this was my idea of how to access the last empty element in the array. Of course this might be wrong as I am new to coding, but from your answer I do not quite understand why is it a problem. Anyways following your first tip helped me a lot, so I'd like to thank you once again. Commented Mar 23, 2020 at 21:06
  • 1
    @TedLyngmo Thank you for the tip! My code looks way better now Commented Mar 23, 2020 at 21:07
  • @MichaelMichael - I'm not quite sure what you mean. If an array has (say) two elements, then accessing the third element gives undefined behaviour. It isn't a magical way to recognise the last element of the array. Commented Mar 24, 2020 at 10:44

1 Answer 1

1

You're accessing the array out of bounds. When items == arrSize, you try to write to ptr[lastElement], which, since lastElement and items will have the same value, will write to ptr[arrSize] which is past the end of the allocated space.

There are also numerous problems in your array expansion code, and you're passing parameters that you don't need to.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It helped me to fix the code! As for your second remark, what parameters are not required?
@MichaelMichael You're passing variables to func that should be locals instead (like temp).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.