-1

Im trying to understand pointers but im having trouble increasing the size of an array, what im trying to do is make an array that is one bigger than the standard array, copy over everything there, then delete the old pointer array, create a new pointer array that is the right size and move back everything from this temp array. For some reason I keep getting "Unable to read memory" and I have no clue why.

#include <iostream>

int main()
{
    int number;
    int arraySize = 3;
    bool full;
    int *ptr = new int[arraySize] {0};

do
{
    full = false;
    std::cout << "Input a number please: ";
    std::cin >> number;

    getchar();

    for (int i = 0; i < arraySize; i++)
    {
        if (ptr[i] == 0)
        {
            ptr[i] = number;
            i = arraySize;
        }
        else if(arraySize -1 == i)
        {
            full = true;
        }
    }
    if (full == true)
    {
        int *tempPtr = new int[arraySize+1];
        for (int x = 0; x < arraySize; x++ )
        {
            tempPtr[x] = ptr[x];

        }
        delete[] ptr;
        arraySize++;
        int *ptr = new int[arraySize];
        for (int x = 0; x < arraySize; x++)
        {
            ptr[x] = tempPtr[x];

        }
        ptr[arraySize] = number;

    }



}while(number != -1);
for (int z = 0; z < arraySize; z++)
{
    std::cout << ptr[z] << std::endl;
}

getchar();
return 0;

}

filipanton1
  • 59
  • 1
  • 1
  • 9
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Nov 11 '18 at 10:30
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Jesper Juhl Nov 11 '18 at 10:31
  • Every time you are doing `int *ptr = new int[ArraySize]` the result is creating a different variable with the same name, which disappears as soon as it passes out of scope. Only do that once. After the first time, only assign to `ptr` rather than defining a new variable with that name i.e. `ptr = new int[ArraySize]`. – Peter Nov 11 '18 at 10:31
  • @Peter but I have to create one after I have deleted it? – filipanton1 Nov 11 '18 at 10:34
  • @filipanton1 - no you don't. The `ptr = new int [ArraySize]` allocsates memory, `delete [] ptr` releases the memory allocated using operator `new`. It doesn't destroy the pointer `ptr` itself. – Peter Nov 11 '18 at 10:42

1 Answers1

0
int *ptr = new int[arraySize];

This will not be the same ptr as the outer ptr so it will leak when it goes out of scope. It should be

ptr = new int[arraySize];

Also, don't forget to delete[] ptr before you exit the program. Here's a slightly modified version that is easier to read i.m.o.

#include <iostream>

int main()
{
    int number;
    int arraySize = 3;
    int *ptr = new int[arraySize] {0};
    int i=0;

    do
    {
        std::cout << "Input a number please: ";
        std::cin >> number;
        std::cin.ignore();
        if(number==-1) break; // no need to increase array

        if(i>=arraySize) {
            // the array is full
            int *tempPtr = new int[arraySize+1];
            for (int x = 0; x < arraySize; ++x) tempPtr[x] = ptr[x];
            delete[] ptr;

            // just assign the address tempPtr is pointing at to ptr
            ptr = tempPtr;
            ++arraySize;
        }
        // store the new number  
        ptr[i] = number;
        ++i;
    } while(true);

    for (int z = 0; z < i; z++)
    {
        std::cout << ptr[z] << std::endl;
    }
    delete[] ptr;

    getchar();
    return 0;
}

Note that there are standard containers (std::vector etc) dealing with dynamic allocation more efficiently than this so you don't have to write it yourself.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Hmm I did that now but I still seem to have problem there, when I have entered 5 numbers it says "HEAP CORRUPTION DETECTED", do you know what that could mean? – filipanton1 Nov 11 '18 at 10:52
  • No idea... I didn't get that. I'll put my edited code in the answer. – Ted Lyngmo Nov 11 '18 at 11:03
  • Generelly it's not necessary to delete memory before exiting a program. In the words of Raymond Chen: "The building is being demolished. Don't bother sweeping the floor and emptying the trash cans and erasing the whiteboards." (https://blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683) – Geier Nov 11 '18 at 12:15
  • @Geier true, but in the context of trying to understand pointers as OP says, it's probably wise to have one delete for every new. – Ted Lyngmo Nov 11 '18 at 12:23