Explain a line of C code in qsort

I looked at various qsort implementations and found the string found here ( https://code.woboq.org/userspace/glibc/stdlib/qsort.c.html ), which I don’t understand. It looks like a function pointer declaration. I would be grateful for any help. I have included as much code as needed (with a highlighted line) to answer the question. Please let me know if not, thanks.

typedef struct
{
    char *lo;
    char *hi;

} stack_node;


void _quicksort (void *const pbase, size_t total_elems, size_t size, cmp_t cmp, void *arg)
{

    char *base_ptr = (char *) pbase;

    const size_t max_thresh = 4 * size;

    if (total_elems == 0)

        return;

    if (total_elems > 4)
    {
        char *lo = base_ptr;
        char *hi = &lo[size * (total_elems - 1)];
        stack_node stack[(8 * sizeof(size_t))];
        stack_node *top = stack;

        /* Line below is a function pointer declaration?  Initializes struct? */

        ((void) ((top->lo = (((void*)0))), (top->hi = (((void*)0))), ++top));

        while ((stack < top))
        {
            char *left_ptr;
            char *right_ptr;

            char *mid = lo + size * ((hi - lo) / size >> 1);

... the code

+4
source share
2 answers

No, this is not a function pointer declaration. This is just a tricky way to say

top->lo = 0;
top->hi = 0;
++top;

You can rewrite the above as a single expression using the operator ,

top->lo = 0, top->hi = 0, ++top;

then add unnecessary drops

top->lo = (void *) 0, top->hi = (void *) 0, ++top;

and a set of redundant ()s

(top->lo = (((void *) 0))), (top->hi = (((void *) 0))), ++top;

(void) (, , "" )

((void) ((top->lo = (((void *) 0))), (top->hi = (((void *) 0))), ++top));

.

- , () . . , ? ((void *) 0) NULL.

+7

URL-, , ,

/* The next 4 #defines implement a very fast in-line stack abstraction. */
/* The stack needs log (total_elements) entries (we could even subtract
   log(MAX_THRESH)).  Since total_elements has type size_t, we get as
   upper bound for log (total_elements):
   bits per byte (CHAR_BIT) * sizeof(size_t).  */

#define STACK_SIZE        (CHAR_BIT * sizeof(size_t))
#define PUSH(low, high)   ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
#define POP(low, high)    ((void) (--top, (low = top->lo), (high = top->hi)))
#define STACK_NOT_EMPTY   (stack < top)

PUSH, POP. () , ++top --top inline .

, , , (C) 1991 - 2017 qsort.c... 1991 , , .

+2

Source: https://habr.com/ru/post/1690161/


All Articles