Thread: sorting algo causing seg fault

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    7

    sorting algo causing seg fault

    any ideas on how I could fix this seg fault would be great! thanks!

    Code:
    template <typename I>
    void merge(I begin, I mid, I end, bool (*compare)(typename I::value_type &x, typename I::value_type &y)) {
    	int n1 = mid - begin;
    	int n2 = end - mid;
    
    	typedef typename I::value_type T;
    	T* left = new T[n1];
    	T* right = new T[n2];
    	int i;
    
    	//populate left and right arrays
    	i = 0;
    	for (I p=begin;i<n1;p++) {
    		left[i] = *p;
    		i++;
    	}
    	i = 0;
    	for (I p=(mid);i<n2;p++) {
    		right[i] = *p;
    		i++;
    	}
    
    	T* result;
    	i = 0;
    	int leftCtr = 0;
    	int rightCtr = 0;
    
    	//Merge the two sorted arrays
    	for (;i<n1+n2;i++) {
    		if (leftCtr<n1 && rightCtr<n2) {
    			if (compare(left[leftCtr],right[rightCtr])) {
    				result[i] = left[leftCtr++];
    			}
    			else result[i] = right[rightCtr++];
    		}
    		else if (leftCtr<n1)
    			result[i] = left[leftCtr++];
    		else
    			result[i] = right[rightCtr++];
    	}
    	i = 0;
    
    	//Copy the sorted array back into the original array.
    	for (I p=begin;p!=end;p++) {
    		*p = result[i++];
    	}
    }
    
    
    
    template<typename I>
    void mergeSort(I begin, I end, bool (*compare)(typename I::value_type &x, typename I::value_type &y)) {
    
    	int len = end - begin;
    
    	if (len>1) {
    		I mid = begin + (len/2);
    
    		//Sort both the halves
    		mergeSort(begin, mid, compare);
    		mergeSort(mid, end, compare);
    		merge(begin, mid, end, compare);
    	}
    
    	return;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You don't appear to allocate any memory for result.

    (On the other hand it is unclear why you allocate extra arrays to make a copy of the ranges to be merged.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Here's a better idea: compile the code in debug mode and run it in the debugger. When the seg fault occurs the debugger will stop when it occurs and you can evaluate the state of the program at the time of the crash. Where you're seg faulting, I'm going to assume you're on a *nix system and using gcc, in which case you would use gdb as your debugger. Debugging with GDB

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Quote Originally Posted by anon View Post
    You don't appear to allocate any memory for result.

    (On the other hand it is unclear why you allocate extra arrays to make a copy of the ranges to be merged.)
    thanks alot! that was exactly the problem.

    ragstoriches - you are correct as well. when i ran it with gdb it was pretty apparent. strangely when i ran it with o3 optimization the gdb was actually more helpful than when i compiled without any optimizers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ltoa causing seg fault!
    By boblettoj99 in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 07:30 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. sorting list gettin seg fault
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 05-20-2006, 11:52 PM
  4. lsass is causing a seg fault
    By skorman00 in forum Tech Board
    Replies: 3
    Last Post: 07-02-2004, 09:33 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM