问题
I have written down the following program that uses the quicksort algorithm to sort how ever many ints are put into the command line using linked lists. Not only am I getting an ISO C90 error about mixed declarations but there is a memory leak somewhere in my code and I am not sure how to fix it. Any help would be appreciated!
#include <stdio.h>
#include "linked_list.h"
#include <stdlib.h>
#include "memcheck.h"
#include <string.h>
#include <assert.h>
node *quicksort(node *list);
int ListLength (node *list);
int main(int argc, char *argv[]) {
if (argc == 1) {
fprintf(stderr, "usage: %s [-q] number1 number2 ... \
(must enter at least one argument)\n", argv[0]);
exit(1);
}
node *list;
node *sorted_list;
int i;
int intArg = 0; /* number of integer arguments */
int print = 1;
/* if -q is found anywhere then we are going
* to change the behavior of the program so that
* it still sorts but does not print the result */
for ( i = 1 ; i < argc; i++) {
if (strcmp(argv[i], "-q") == 0) {
print = 0;
}
else {
list = create_node(atoi(argv[i]), list); /* memory allocation in the create_node function */
intArg++; }
}
if (intArg == 0) {
fprintf(stderr, "usage: %s [-q] number1 number2 ...\
(at least one of the input arguments must be an integer)\n", argv[0]);
exit(1); }
sorted_list = quicksort(list);
free_list(list);
list = sorted_list;
if (print == 1) {
print_list(list); }
print_memory_leaks();
return 0; }
/* This function sorts a linked list using the quicksort
* algorithm */
node *quicksort(node *list) {
node *less=NULL, *more=NULL, *next, *temp=NULL, *end;
node *pivot = list;
if (ListLength(list) <= 1) {
node *listCopy;
listCopy = copy_list(list);
return listCopy; }
else {
next = list->next;
list = next;
/* split into two */
temp = list;
while(temp != NULL) {
next = temp->next;
if (temp->data < pivot->data) {
temp->next = less;
less = temp;
}
else {
temp->next = more;
more = temp;
}
temp = next;
}
less = quicksort(less);
more = quicksort(more); }
/* appending the results */
if (less != NULL) {
end = less;
while (end->next != NULL) {
end = end->next;
}
pivot->next = more;
end->next = pivot;
return less; }
else {
pivot->next = more;
return pivot; } }
int ListLength (node *list) {
node *temp = list;
int i=0;
while(temp!=NULL) {
i++;
temp=temp->next; }
return i; }
回答1:
In main
, you free one node, the original head of the list:
sorted_list = quicksort(list);
free_list(list);
But you never free any other node, although you copy the nodes. So all the original list nodes save from the first are floating in unreachable memory. Either free
on copy, but then don't free
in main
, or don't copy at all (and free all nodes in main
, but only after you no longer need them).
回答2:
Well, you haven't posted the code for free_list()
or create_node()
which are prime candidates for potential memory leaks, but I believe your quicksort()
code has a memory leak here:
less = quicksort(less);
more = quicksort(more); }
if either list has only one element, then this code:
if (ListLength(list) <= 1) {
node *listCopy;
listCopy = copy_list(list);
return listCopy; }
returns a copy of the one element. By setting the less
and more
pointers here, you potentially lose a single node.
Consider the list: 2 1 3
The code would add 1
to the less
list, and 3
to the more
list. Then it would perform quicksort()
on these two one-element lists, return a copy of the list, and lose the pointers to the original less
and more
lists, resulting in a memory leak of two nodes.
Coincidentally, it would be better to replace the check of the if
statement above with:
if (list == NULL || list->next == NULL) {
This avoids having to traverse a potentially long list just to check if it only contains 0 or 1 elements.
来源:https://stackoverflow.com/questions/9423885/quicksort-with-linked-lists