Pointer To Functions = Seg Fault

This is a discussion on Pointer To Functions = Seg Fault within the C++ Programming forums, part of the General Programming Boards category; My problem is, when i call int (*comp)(...) it seg faults on me. I haven't used pointers to functions all ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Pointer To Functions = Seg Fault

    My problem is, when i call int (*comp)(...) it seg faults on me. I haven't used pointers to functions all that much, and ecspecially not when the target function is a class member - even more so, a template.

    Since you're gonna look at my code anyway and if you have extra time:
    1. Most importantly, how do i fix the seg fault?
    2. Does this look like i'm on the right track?
    3. How do i initialize the first 3 variables in my code? (i want to make the private, static, const members)
    4. Do you know where i can get some data to test this with? (perhaps a flat file with a couple thousand records)

    btw, i know i'll need some destructors

    Code:
    #ifndef BTREE_H
    #define BTREE_H
    
    //work around - need to fix this  (seems to be soemthing about it being a template)
    	const short comp_less = -1;
    	const short comp_geater = +1;
    	const short comp_equal = 0;
    
    template<typename _T = int>
    class BTree
    {
    	typedef int key_type;
    	public:
    	BTree(void);
    	BTree(const BTree &, const void * = NULL);
    	BTree(const _T &, const void * = NULL);
    
    	key_type insert(const _T &);
    
    	private:
    	_T  data;
    	key_type key;
    
    	BTree * right;
    	BTree * left;
    
    	int (*comp)(const _T &, const _T &);
    	static inline int compare(const _T &, const _T &);
    	static int __defaultCompare(const _T &, const _T &);
    	
    	
    };
    
    
    template<typename _T>
    BTree<_T>::BTree(const _T &srcNode, const void *compareFunction)
    {
    	if(!compareFunction)
    		comp = BTree<_T>::__defaultCompare;
    	data = srcNode;
    	right = left = NULL;
    }
    
    
    template<typename _T>
    BTree<_T>::key_type BTree<_T>::insert(const _T &srcNode)
    {
    	BTree *side = NULL;
    	//SEGFAULT ON CALL TO COMP
    	// FROM DECLARATION int (*comp)(const _T &, const _T &);
    	// FROM CONSTRUCTOR: comp = __defaultCompare;
    	// FROM DECLARATION static int __defaultCompare(const _T &, const _T &);
    	
    	if(comp(srcNode, data) == comp_less)
    		side = left;
    	else
    		side = right;
    	
              //following commented out to find seg fault
    	/*if(!side)
    		side = new BTree(srcNode, comp);
    	else
    		side->insert(srcNode);*/
    
    	return 0;
    }
    
    
    
    template<typename _T>
    int BTree<_T>::__defaultCompare(const _T &a, const _T &b)
    {
    	if(a > b)
    		return +1;
    	else if(a < b)
    		return -1;
    	else
    		return 0;
    
    	return 0;
    }
    
    #endif
    Last edited by misplaced; 04-05-2005 at 01:10 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    nevermind..........i found it.............i guess actually assigning a value to comp some how magically makes everything work.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    >> const void *compareFunction
    Make that a real type while you're in there.

    gg

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    yeah - i'm not even sure why i did that.......

    i realized that shortly after i posted
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Replies: 2
    Last Post: 09-09-2007, 12:58 AM
  4. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  5. seg fault on unix platform
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-08-2001, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21