Swapping in a char * array[ ] leading to issues

2019-07-25 13:21发布

问题:

void sortArray(char* array[], int count){
int compare;
int index;
 for(index = 0; index < count; index++){ //runs through array, keeping track of index
   for(compare = index; compare < count; compare++){
        if (strcmp(array[compare] , array[index]) <= 0){
             swap(*(array+compare), *(array+index));
        }
   }
 }
}
void swap(char *strA, char *strB){
    char *temp = (char *)malloc((strlen(strA)+1) * sizeof(char));
    assert(temp!=NULL);
    strcpy(temp, strA);
    free(strA);
    strA=(char *)malloc((strlen(strB)+1) * sizeof(char));
    assert(strA!=NULL);
    strA=strcpy(strA, strB);
    free(strB);
    strB= (char *)malloc((strlen(temp)+1) * sizeof(char));
    assert(strB!=NULL);
    strcpy(strB, temp);
    free(temp);
}

gives output:

Array1[0]: Adam Pendleton
Array1[1]: Jeison Ortega
Array1[2]: Theodore Staake
Array1[3]: Patrick Mahoney
Array1[4]: Andrew Silberstein
Array1[5]: Alan Boulais
Array1[6]: Daniel Comeau
Array1[7]: Sean Mikhael
Array1[8]: Sarah Shank
Array1[9]: Daniel Verge
Array1[10]: Dhimitris Natsis
Array1[11]: Kathleen Lent
Array1[12]: Osei Collins
Array1[13]: Jason Hintlian
Array1[14]: Michael Gibson
Array1[15]: Alex Grossi
Array1[16]: Michael Svedberg
Array1[17]: Randall White
Array1[18]: Alvin Cordor
Array1[19]: Rodrigo Roldan
Array1[20]: Matthew McArthur
Array1[21]: Jesse Anaya
    Sorted Array1[0]: Adam Pendleton
    Sorted Array1[1]: Patrick Mahoney
    Sorted Array1[2]: Theodore Staake
    Sorted Array1[3]: Sarah Shank
    Sorted Array1[4]: Dhimitris Natsis
    Sorted Array1[5]: Alan Boulais
    Sorted Array1[6]: Alex Grossi
    Sorted Array1[7]: Alvin Cordor
    Sorted Array1[8]: Sean Mikhael
    Sorted Array1[9]: Osei Collins
    Sorted Array1[10]: Michael Svedberg
    Sorted Array1[11]: Daniel Comeau
    Sorted Array1[12]: Daniel Verge
    Sorted Array1[13]: Jason Hintlian
    Sorted Array1[14]: Jesse Anaya
    Sorted Array1[15]: Michael Gibson
    Sorted Array1[16]: Matthew McArthur
    Sorted Array1[17]: Randall White
    Sorted Array1[18]: Rodrigo Roldan
    Sorted Array1[19]: Kathleen Lent    <-----not sure why this is dupe
    Sorted Array1[20]:                  <-----not sure why this is here
    Sorted Array1[21]: Kathleen Lent

Im not sure where these blank spaces are coming from or why there are duplicate names. I have even created another function called reverseArray that just swapped first and last strings and it seems like the issue stems from the swap function, my guess is that when it is copying the strings there is some mess up there.

For this is just a small portion of what is going on, i have read in text from a file line by line to fill the char* array[] with strings of names, this works fine as evidenced by the array printing correctly before it is sorted.

回答1:

I'm not sure about your exact issue yet, but you can do a far simpler and more efficient job by swapping the strings' locations in your array, instead of changing the strings themselves:

void sortArray(char* array[], int count) {
    int compare;
    int index;
    char *tmp;
    for(index = 0; index < count; index++) {
        for(compare = index; compare < count; compare++) {
            if (strcmp(array[compare], array[index]) <= 0) {
                tmp = array[compare];
                array[compare] = array[index];
                array[index] = tmp;
            }
        }
    }
}


回答2:

The exact problem with your code is that your swap() function is freeing the two pointers passed into it, and does not return pointers to the new memory it allocates in any form. As a result, all of the pointers in the array you're sorting end up pointing to memory that has been freed -- it's a wonder that it works at all.

To fix this, you could change the signature of swap() to take the two pointers by reference:

void swap (char **strA, char **strB)

and pass pointers in to it as:

swap(array+compare, array+index);

That being said, Claudiu's suggestion that you just swap pointers instead of copying data around is probably a much better idea in the long run. :)



标签: c swap arrays