Hi all,

My code will only run on arrays that are 4, 8, 16... in size, and i can't understand why. it crashes at runtime when running on arrays whose size is 5 or 6 for example.

Any help is welcomed.

Code:#include <stdio.h> #include <stdlib.h> /* pointerSort: ----------- Description: Sorts an array of pointers based on the values of the integers they are pointing to. Input: int* arr - The array that will be used to sort the pointers by. int size - The size of the above array. Output: int** - Returns a sorted pointer array. */ int** pointerSort(int* arr, int size); /* mergeSort: ---------- Description: Sort an pointer array recursively. Input: int** pointArr - The array of pointers that will be sorted. int size - The size of the above array. Output: int** pointArr - A sorted pointer array. */ void mergeSort(int** pointArr, int size); /* mergeArrays: ------------ Description: Merges 2 given pointer arrays into a third array, while sorting them. Input: int** arr1 - The first pointer array to merge. int size1 - The size of the above array. int** arr2 - The second pointer array to merge with the first. int size2 - The size of the above array. int** resultArr - The resulting array, a sorted merge of arr1 and arr2. Output: int** resultArr - A sorted pointer array. */ void mergeArrays(int** arr1, int size1, int** arr2, int size2, int** resultArr); /* copyArr: -------- Description: Copies the values of one array to another. Input: int** destArr - The array who's data will be overwritten. int** srcPArr - The array to copy from. int size - The size of both of the above arrays. Output: int** destArr - Will now have new values. */ void copyArr(int** destArr, int** srcPArr, int size); void main(){ int nums[8] = { 7, 2, 5, 9, 6, 13, 1, 19 }; int** resultArr; int size, i; size = 8; resultArr = pointerSort(nums, size); for ( i = 0 ; i<size ; i++ ) printf("%d, ", *resultArr[i]); } int** pointerSort(int* arr, int size){ int i; int **pArr; pArr = (int**)malloc(size * sizeof(int)); if (pArr != NULL){ for ( i = 0 ; i < size ; i++ ) pArr[i] = &arr[i]; mergeSort(pArr, size); } return pArr; } void mergeSort(int** pointArr, int size){ int** tempPArr; if (size == 1) return; else{ mergeSort(pointArr, size/2); mergeSort(pointArr + size/2, size/2); tempPArr = (int**)malloc(size*sizeof(int)); if (tempPArr != NULL){ mergeArrays(pointArr, size/2, pointArr+size/2, size/2, tempPArr); copyArr(pointArr, tempPArr, size); free(tempPArr); } } } void mergeArrays(int** arr1, int size1, int** arr2, int size2, int** resultArr){ int ind1, ind2, write; ind1 = ind2 = write = 0; while (ind1 < size1 && ind2 < size2){ if ( *arr1[ind1] < *arr2[ind2] ){ resultArr[write] = arr1[ind1]; write++; ind1++; } else{ // (*arr1[ind1] >= *arr2[ind2]) resultArr[write] = arr2[ind2]; write++; ind2++; } } while ( ind1 < size1 ){ resultArr[write] = arr1[ind1]; write++; ind1++; } while ( ind2 < size2 ){ resultArr[write] = arr2[ind2]; write++; ind2++; } } void copyArr(int** destArr, int** srcPArr, int size){ int i; for ( i = 0 ; i < size ; i++ ) destArr[i] = srcPArr[i]; }