Thread: Sort array seg fault

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Sort array seg fault

    Although I could use std::sort() I decided to attempt to sort a string array using bubble sort.

    It compiles but I get a seg-fault noted below at a certain point. The exact same algorithm worked fine on an integer array, but it doesnt seem to like the strings,
    or is there a more convient sorting method for strings other than cheating with std::sort()?

    Code:
    #include <iostream>
    #include <string>
    
    // function prototype
    void sortNames ( std::string[], const int );
    
    // main function - driver /////////////////////////////////////////////////////
    //
    int main ( void ) {
    	const int ARRAY_SIZE = 20;
    
    	std::string names[ ARRAY_SIZE ] = { "" };
    
    	std::cout << "Enter 20 names: ";
    
    	for ( int i = 0; i < ARRAY_SIZE; i++ ) {
    		std::getline ( std::cin, names[ i ] );
    	}
    
    	sortNames ( names, ARRAY_SIZE );
    
    	std::cout << "\n\nIn Order:\n\n";
    
    	for ( int i = 0; i < ARRAY_SIZE; i++ ) {
    		std::cout << names[ i ] << std::endl;
    	}
    
    	std::cin.get();
    
    	return 0;
    }
    
    // function to print the names out alphabetically
    void sortNames ( std::string nm[], const int SIZE ) {
    
    	for ( int pass = 1; pass < SIZE +1; pass++ ) {
    		for ( int i = 1; i < SIZE +1; i++ ) {
    			if ( nm[ i ] > nm[ i + 1 ] ) {
    				std::string temp = nm[ i ];
    				nm[ i ] = nm[ i + 1 ]; // SEG FAULTS
    				nm[ i + 1 ] = temp;
    			}
    		}
    	}
    }
    Double Helix STL

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You are indexing past the nm[] array on SIZE+1.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    If you are doing any sort of array loop, and then you have an array i+1, then you'll eventually hit--in your case --- 21, which will give you a segmentation fault, because you had an access violation. You should check if it's the last i, before you do an i+1, because you are calling something that may not even exist.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks so much guys.

    Merrry Xmas
    Double Helix STL

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. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM