Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:41:07+00:00 2026-05-27T06:41:07+00:00

I’m trying call qsort in Cython with a custom compare function but I don’t

  • 0

I’m trying call qsort in Cython with a custom compare function but I don’t understand how to pass the function reference. First, I have a struct:

cdef struct Pair:
    int i,j
    float h

The compare function sorts by h:

cdef int compare(const_void *a, const_void *b):
    cdef float v = ((<Pair*>a)).h-((<Pair*>b)).h
    if v < 0: return -1
    if v > 0: return 1
    return 0

This is the part I’m having trouble with:

    cdef Pair[5] pa
    for i in range(5):
        pa[i].i = i;
        pa[i].j = i*2;
        pa[i].h = i*.5;
    qsort(pa,5,sizeof(Pair),compare)

The last line won’t compile and generates this error which I believe is related to the fact that I can’t figure out how to pass compare as a reference to qsort:

Cannot assign type 'int (const_void *, const_void *)' to 'int (*)(const_void *, const_void *) nogil'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:41:08+00:00Added an answer on May 27, 2026 at 6:41 am

    I’ve not been able to reproduce your error. The code you’re using is right, and working with Cython 0.15. The only thing i see that might be your error is the “gil” appended to the type. If you want to specifically declare a imported method as “gil safe”, append “nogil” at the end of the declaration.

    (note that you can check your python code with cython -a , then open web browser for )

    cdef extern from "stdlib.h":
        ctypedef void const_void "const void"
        void qsort(void *base, int nmemb, int size,
                int(*compar)(const_void *, const_void *)) nogil
    
    cdef struct Pair:
        int i,j
        float h
    
    cdef int compare(const_void *a, const_void *b):
        cdef float v = ((a)).h-((b)).h
        print 'do something with', v
        if v  0: return 1
        return 0
    
    def r():
        cdef Pair[5] pa
        for i in range(5):
            pa[i].i = i;
            pa[i].j = i*2;
            pa[i].h = i*.5;
        print 'call qsort'
        qsort(pa,5,sizeof(Pair),compare)
        print 'call qsort done'
    
    r()
    

    This snippet is compiled as:

    $ cython --version
    Cython version 0.15
    $ cython --embed test.pyx
    $ gcc -I/usr/include/python2.7 -Wall -std=c99 test.c -lpython2.7
    $ ./a.out 
    call qsort
    do something with -0.5
    do something with -0.5
    do something with -0.5
    do something with -1.0
    do something with -0.5
    call qsort done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.