### double free in quicksort

in main() (m is 5 and x[] is an array of random ints)
 ``12345678910`` `````` Quick Q; Q.Build(x, m); int* x4 = Q.Sort(); cout << endl << endl << "Quick Comparisons: " << Q.comparisons; cout << endl << "Sorted array:" << endl; for(int i = 0; i < m; i++){ if(i == m/2 + 1){cout << endl;} cout << x4[i] << " "; }``````

My error is double free, and I get it while I'm printing out x4, so it's after the array has already been built and sorted (build just does class assignments s.t. A = x and n = m).

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````int* Quick::Sort(){ RecursiveSort(0, n - 1); return A; } void Quick::RecursiveSort(int lo, int hi){ comparisons++; if(lo < hi){ int pIndex = Partition(lo, hi); RecursiveSort(lo, pIndex - 1); RecursiveSort(pIndex + 1, hi); } } int Quick::Partition(int lo, int hi){ int pivot = A[lo]; int left = lo; for(int i = lo + 1; i <= hi; i++){ comparisons += 2; if(A[i] < pivot){ left++; int k = A[i]; A[i] = A[left]; A[left] = k; } } comparisons++; int k = A[left]; A[left] = A[lo]; A[lo] = k; return left; }``````

Weird because I'm never deleting anything?
Does Q.Build() copy the content of x, or only the pointer ?
Just the pointer. Also, the same pointer is used for implementations of mergesort, heapsort, and insertion sort in the same program (with mergesort and insertion sort copying the pointer).
Topic archived. No new replies allowed.