seg fault with simple function template

This is a discussion on seg fault with simple function template within the C++ Programming forums, part of the General Programming Boards category; hey everyone, Here's some code I wrote, and i was woundering why i was getting a segfault... im just simply ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    seg fault with simple function template

    hey everyone,

    Here's some code I wrote, and i was woundering why i was getting a segfault...

    im just simply merging two vectors, with no repeats..

    Code:
    template <class T>
    vector<T> merge(const vector<T> &c1, const vector<T> &c2) {
    
    	vector<T> final;
    	bool valid = true;
    
    	for(typename vector<T>::const_iterator k = c1.begin(); k!=c1.end(); ++k) {
    		final.push_back(*k);
    	}
    	
    	for(typename vector<T>::const_iterator i = c2.begin(); i!=c2.end(); ++i) {
    		for(typename vector<T>::const_iterator j = final.begin(); i!=final.end(); ++j) {
    			if(*i==*j) {
    				valid=false;
    				break;
    			}
    		}
    		if(valid)
    			final.push_back(*i);
    
    		valid=true;
    	}
    
    	return final;
    }
    Code:
    int main(void) {
    	
    	vector<int> c1;
    	vector<int> c2;
    
    	c1.push_back(1);c1.push_back(2);c1.push_back(3);
    	c2.push_back(1);c2.push_back(4);c1.push_back(5);
    	
    	vector<int> final = merge<int>(c1,c2);
    	
    	for (vector<int>::iterator i = final.begin(); i!=final.end(); ++i)
    		cout << *i << endl;
    	
    	return 0;
    }

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    This works fine for me on Windows with VC7:

    Code:
    #include <iostream>
    #include <vector>
    
    template <class T>
    std::vector<T> merge(const std::vector<T> &c1, const std::vector<T> &c2) {
    
        std::vector<T> final;
        bool valid = true;
    
        for(typename std::vector<T>::const_iterator k = c1.begin(); k!=c1.end(); ++k) {
            final.push_back(*k);
        }
    
        for(typename std::vector<T>::const_iterator i = c2.begin(); i!=c2.end(); ++i) {
            for(typename std::vector<T>::const_iterator j = final.begin(); i!=final.end(); ++j) {
                if(*i==*j) {
                    valid=false;
                    break;
                }
            }
            if(valid)
                final.push_back(*i);
    
            valid=true;
        }
    
        return final;
    }
    
    
    int main(void) {
    
        std::vector<int> c1;
        std::vector<int> c2;
    
        c1.push_back(1);c1.push_back(2);c1.push_back(3);
        c2.push_back(1);c2.push_back(4);c1.push_back(5);
    
        std::vector<int> final = merge<int>(c1,c2);
    
        for (std::vector<int>::iterator i = final.begin(); i!=final.end(); ++i)
            std::cout << *i << std::endl;
    
        return 0;
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    hmmm thats very odd...its still giving me this error in g++
    Code:
    Segmentation fault (core dumped)
    i had to re-write the function to make it work

    the following works fine
    Code:
    template<class T>
    vector<T> merge(const vector<T> &c1, const vector<T> &c2) {
    	vector<T> final;
    
    	copy(c1.begin(), c1.end(), back_inserter<vector<T > >(final));
    	
    	for(typename vector<T>::const_iterator i = c2.begin(); i!=c2.end(); ++i) {
    		if(!binary_search(final.begin(),final.end(),*i))
    			final.push_back(*i);
    	}
    
    	return final;		
    }
    Anyone got an suggestions on how to make my orignal code work on g++...?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,301
    The problem is that, in the inner loop, you are testing i!=final.end(). That test should be j!=final.end().

    Incidentally (unrelated to the segmentation fault): if c1 includes duplicates, the end result of the merge function will still include those duplicates.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in function. HELP!
    By wuzzo87 in forum C Programming
    Replies: 7
    Last Post: 09-28-2006, 06:48 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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