X-Git-Url: https://git.octo.it/?a=blobdiff_plain;ds=sidebyside;f=src%2Fsn_stage.c;h=f0d169db6c4298c88987fee9ee954ccb1e892ba0;hb=f70ce78fe7c255789636ddfbf57d2fb8fede9576;hp=0cfabe92108c27706dffa5352b3e0aab42cec25d;hpb=02dd5a8e9890f7b16b950db238eeff42a57e9142;p=sort-networks.git diff --git a/src/sn_stage.c b/src/sn_stage.c index 0cfabe9..f0d169d 100644 --- a/src/sn_stage.c +++ b/src/sn_stage.c @@ -64,6 +64,7 @@ int sn_stage_comparator_remove (sn_stage_t *s, int c_num) sn_comparator_t *temp; assert (c_num < s->comparators_num); + assert (c_num >= 0); if (nmemb > 0) memmove (s->comparators + c_num, s->comparators + (c_num + 1), @@ -71,11 +72,19 @@ int sn_stage_comparator_remove (sn_stage_t *s, int c_num) s->comparators_num--; /* Free the unused memory */ - temp = (sn_comparator_t *) realloc (s->comparators, - s->comparators_num * sizeof (sn_comparator_t)); - if (temp == NULL) - return (-1); - s->comparators = temp; + if (s->comparators_num == 0) + { + free (s->comparators); + s->comparators = NULL; + } + else + { + temp = (sn_comparator_t *) realloc (s->comparators, + s->comparators_num * sizeof (sn_comparator_t)); + if (temp == NULL) + return (-1); + s->comparators = temp; + } return (0); } /* int sn_stage_comparator_remove */