Sort array seg fault

This is a discussion on Sort array seg fault within the C++ Programming forums, part of the General Programming Boards category; Although I could use std::sort() I decided to attempt to sort a string array using bubble sort. It compiles but ...

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

    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;
    			}
    		}
    	}
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are indexing past the nm[] array on SIZE+1.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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,475
    Thanks so much guys.

    Merrry Xmas
    I'm just trying to be a better person - My Name Is Earl

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, 08:51 AM
  4. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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