0

When I run this code:

#include <iostream>

using namespace std;

void makeArrayBigger(int*, int&, int);
void arrayIni(int*, int, int);
void displayArray(int*, int);

int main(){

    int size = 5;
    int *arr = new int[5];

    arrayIni(arr, size, 0);
    //displayArray(arr, size);


    makeArrayBigger(arr, size, 8);
    //displayArray(arr, size);


    arrayIni(arr, size, 1);

    system("pause");
    return 0;

}

void makeArrayBigger(int *arr, int &size, int newSize){
    int *newArr = new int[newSize];

    for (int counter = 0; counter < size; counter++){
        newArr[counter] = arr[counter];
    }

    delete[] arr;
    arr = new int[newSize];
    for (int index = 0; index < size; index++){
        arr[index] = newArr[index];
    }
    delete[] newArr;
    size = newSize;
}

void arrayIni(int *arr, int size, int ini){

    for (int index = 0; index < size; index++){
        arr[index] = ini;
    }
}

void displayArray(int *arr, int size){

    if(arr == NULL)
        return;
    for (int counter = 0; counter < size; counter++){
        cout << arr[counter] << endl;
    }

    cout << endl;
}

I get this breakpoint error:

Windows has triggered a breakpoint in pointer.exe.

This may be due to a corruption of the heap, which indicates a bug in pointer.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while pointer.exe has focus.

user1721803
  • 1,125
  • 2
  • 14
  • 21
  • 1
    Did you look at the stack trace in the debugger to see where the originating call site in your code is? That should give you a good indication of where the problem is. – Captain Obvlious Feb 25 '14 at 03:09
  • HEAP[pointer.exe]: HEAP: Free Heap block 6a4d30 modified at 6a4d58 after it was freed – user1721803 Feb 25 '14 at 03:13
  • This should be a good debugging exercise. Try stepping through the code or look at the stack trace like @CaptainObvlious mentioned. You might be trying to use a variable after freeing it. – sraok Feb 25 '14 at 03:14
  • check the address of arr in main function and in makeArrayBigger function. I think that will help you find the error... – HadeS Feb 25 '14 at 03:14
  • the error happens after the second arrayIni() – user1721803 Feb 25 '14 at 03:16
  • `int* makeArrayBigger(int*, int&, int);` use this signature and return `arr` and assign it to `arr` in main function. See if this works... – HadeS Feb 25 '14 at 03:26
  • 1
    makeArrayBigger needs to have a reference (or pointer to pointer) for the first parameter. Currently it's creating a larger local instance of arr. There's no need for a reference to size. The function can be void makeArrayBigger(int* &, int, int); – rcgldr Feb 25 '14 at 03:28

2 Answers2

1

The reason of this error is that you confused the conception 'Pass by value' with 'Pass by Reference'. The answer of the following question might be an good reference:

What's the difference between passing by reference vs. passing by value?

The problem will be fixed by replacing the void makeArrayBigger() interface with the following codes

void makeArrayBigger(int* &arr, int &size, int newSize);

Update:

when I pass a pointer I'm passing a pointer that points to that same location.

It is the same location but not the same pointer. Whether delete or malloc the new pointer will not change the value of the actual parameter.

In this case, the arr pointer keeps the same size after makeArrayBigger(). But it will be assumed to be the new size(8) in arrInit().

Community
  • 1
  • 1
Steve
  • 1,448
  • 11
  • 18
  • I know the difference between passing by reference and passing by value. From what I understand though (incorrectly) is that when I pass a pointer I'm passing a pointer that points to that same location. If that is the case then why pass the pointer by reference? – user1721803 Feb 25 '14 at 04:57
  • @user1721803. `The same location` does not means the same pointer. More information have been updated. – Steve Feb 25 '14 at 06:13
  • I think I get it now. Since I am deleteing arr in makeArrayBigger I am deleting what arr in main is pointing to and assigning the newly allocated memory to arr in makeArrayBigger which after the function is finished that pointer is deleted although the allocated memory is still in memory. And arr in main is now pointing to nothing. Correct me if I'm wrong. – user1721803 Feb 25 '14 at 14:13
  • @user1721803. The concept of **SCOPE** shall be cleared while programming. The parameters of `arr` are different in different scopes of `makeArrayBigger()` and `main()`. You can tell this by comparing the address of `arr`. In short, `arr` in main won't be changed unless it is the same parameters in `main()` and `makeArrayBigger()`. – Steve Feb 26 '14 at 04:51
1

Pass in a reference to the array (int*) just like you pass a reference to the array size (int). Then you can replace the array with a new array and update the size as you are currently doing:

void resize (int*& array, int& arraySize, int newSize)
{
    delete [] array;

    arraySize = newSize;
    array = new int [arraySize];
}

int main() {

    int testSize = 5;
    int* testArray = new int [testSize];

    // Do stuff

    resize (testArray, testSize, 8);

    // Do more stuff

    delete [] testArray;

    testArray = NULL;
    testSize = 0;

    return 0;
}
Chris
  • 2,655
  • 2
  • 18
  • 22