Segmentation fault: vector to function

This is a discussion on Segmentation fault: vector to function within the C++ Programming forums, part of the General Programming Boards category; Can some one please tell me why I'm getting a 'Segmentation fault' on this code? Code: #include <iostream> #include <vector> ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    25

    Segmentation fault: vector to function

    Can some one please tell me why I'm getting a 'Segmentation fault' on this code?

    Code:
    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    
    int radixSort(vector<long> &, int);
    
    int main(){
    
      vector<long> to_Sort;
      long int_Read = 0;
    
      while(cin >> int_Read){
        cin >> int_Read;
        to_Sort.insert(to_Sort.begin(), int_Read);
      }
    
      cout << to_Sort.size() << " ";
    
      radixSort( to_Sort, to_Sort.size() );
    
      cout << to_Sort[0] << endl;
    
    
      return 0;
    
    }
    
    int radixSort( vector<long> & to_Sort, int list_Size ){
    
      int i, j, k, cur_Factor;
      const int max_Digits = 10;
    
      queue<long> pass_Qu[max_Digits];
      for( i = 0, cur_Factor = 1; i < max_Digits; cur_Factor *= max_Digits, i++ ){
        for( j = 0; j < list_Size; j++ ){
          pass_Qu[( to_Sort[j] / cur_Factor ) % max_Digits].push( to_Sort[j] );
      
        }
        for( j = k = 0; j < max_Digits; j++ ){
          while( !pass_Qu[j].empty() ){
             to_Sort[k++] = pass_Qu[j].front();
          }
        }
    
    
      }
      return 0;
    }
    Thank you.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Not the segfault, but there's an error here:
    Code:
      while(cin >> int_Read){
        cin >> int_Read;
    The second read is incorrect. Remove it.

    The segfault is because front() doesn't remove the element from a queue, so the refill loop goes on forever, overflowing to_Sort. You need to call pop() in the loop.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. strange segmentation fault
    By Lateralus in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 09:19 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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