The problem is that the isPrime pointer that you define outside of changeArray() does not change to changeArray() . Its value is copied to the call, where the array is allocated, but the newly allocated array pointer is stored only in this temporary variable, which is destroyed when the function exits. After the call, the isPrime pointer in main() still points to the same place as before the call, and therefore delete[] in main() tries to free the freed memory.
To verify this, print the value of isPrime before and after calling changeArray() and the value of the newly allocated isPrime inside changeArray() .
The solution is to pass isPrime by reference:
void changeArray(bool*& isPrime){ delete[] isPrime; isPrime = new bool[10]; }
source share