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

  • SEARCH
  • Home
  • 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 8497391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:06:47+00:00 2026-06-11T00:06:47+00:00

The sorting part: order = (struct order_s **) calloc(pm->len – q, sizeof(struct order_s*)); for

  • 0

The sorting part:

      order = (struct order_s **) calloc(pm->len - q, sizeof(struct order_s*));

      for (i = 0; i < window_pos; ++i) {
              order[i] = (struct order_s *) malloc(sizeof(struct order_s));
              order[i]->pos = i;
              order[i]->good = good[i];
      }
      for (i = window_pos + q; i < pm->len; ++i)
      {
              order[i-q] = (struct order_s *) malloc(sizeof(struct order_s));
              order[i-q]->pos = i;
              order[i-q]->good = good[i];
      }
     qsort(order, pm->len - q, sizeof(struct order_s *), compare);

The compare function:

int compare (const void * a, const void * b)
{
        if ((((const struct order_s *)a)->good - ((const struct order_s *)b)->good) > 0)
                return 1;
        else
                return -1;
}

The stucture:

struct order_s {
  int pos;
  double good;
};

The values:

(gdb) p pm->len
$35 = 20
(gdb) p window_pos 
$36 = 1
(gdb) p q
$37 = 5

Before qsort():

(gdb) p *order[0]
$2 = {pos = 0, good = 1.3238653863672125}
(gdb) p *order[1]
$3 = {pos = 6, good = 0.96180564211148134}
(gdb) p *order[2]
$4 = {pos = 7, good = 1.0684181637005736}
(gdb) p *order[3]
$5 = {pos = 8, good = 0.92113662370476379}

After qsort():

(gdb) n
(gdb) p *order[0]
$6 = {pos = 0, good = 1.3238653863672125}
(gdb) p *order[1]
$7 = {pos = 6, good = 0.96180564211148134}
(gdb) p *order[2]
$8 = {pos = 7, good = 1.0684181637005736}
(gdb) p *order[3]
$9 = {pos = 8, good = 0.92113662370476379}

After the qsort function, the array of structure isn’t sorted right and throws the segment fault later.

  • 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-06-11T00:06:49+00:00Added an answer on June 11, 2026 at 12:06 am

    The compare() function arguments are pointers to elements of the array. In this case, the elements of the array are struct order*, which means the arguments to compare() are struct order**, not struct order*.

    Change to:

    int compare (const void * a, const void * b)
    {
        const struct order_s** aa = a;
        const struct order_s** bb = b;
    
        /* Can two 'order_s' instances never be equal ? */
        if ( (*aa)->good - (*bb)->good) > 0) return 1;
        else  return -1;
    }
    

    From the C99 standard section 7.20.5.2 The qsort function:

    The contents of the array are sorted into ascending order according to a comparison
    function pointed to by compar, which is called with two arguments that point to the
    objects being compared.
    The function shall return an integer less than, equal to, or
    greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For arrays there is a special function for sorting a part of the array
can't get sorting working for a django-tables2 table. class MyModel(models.Model): pid = models.AutoField('id',primary_key =
In DataTable I could sorting with dataTable.DefaultView.Sort = SortField DESC; I'm getting a DataSet
I using jquery sorting ( http://jqueryui.com/demos/sortable/ ). Is it possible to sort (move list)
I'm sorting numerically on a specific column of a ; separated file with: sort
I've a problem sorting an nsarray. The nsarray is filled with core data: NSFetchRequest
I am sorting and sectioning a tableview using the first letter of an array
I am using JS sorting to call this method: def sort params[:piece].each_with_index do |id,
I'm sorting a NSMutableArray that has NSString that contain numbers. This is the code
Possible Duplicate: Sorting an IList in C# I have an IList<Keyword> where Keyword is

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.