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 341931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:45:18+00:00 2026-05-12T10:45:18+00:00

Trying to follow this example. (Section String sorting…) Is there anything obvious that would

  • 0

Trying to follow this example. (Section String sorting…)
Is there anything obvious that would make this crash in stdlib’s qsort.c?
I also tried cstring_cmp with strncmp specifying 30 chars max, much more than I have.
*fileArray[20] seems to be correctly populated with strings.
Thanks.

char* ptr_fileName;  
char* fileArray[20];//number of files  
size_t strings_len;  

ptr_fileName = (char*)malloc((strlen(FindFileData.cFileName)+1)*sizeof(char));
memcpy(ptr_fileName, FindFileData.cFileName, strlen(FindFileData.cFileName)+1);
fileArray[i] = ptr_fileName;



strings_len = sizeof(fileArray) / sizeof(char *);          
qsort(fileArray, strings_len, sizeof(char *), cstring_cmp);   
//crashing in qsort.c

qsort c-string compare function:

/* qsort C-string comparison function */
    int cstring_cmp(const void *a, const void *b)
    {
        const char **ia = (const char **)a;
        const char **ib = (const char **)b;
        return strcmp(*ia, *ib);
        /* strcmp functions works exactly as expected from
        comparison function */
    }
  • 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-12T10:45:18+00:00Added an answer on May 12, 2026 at 10:45 am

    You say you are only filling fileArray with 10 strings, leaving 10 entries uninitialized.

    When you call qsort, you pass 20 as the strings_len argument.
    This will, of course, result in undefined behavior.

    You have to give qsort accurate information.

    If you are passing 10 strings in the array, you must also pass the number 10 as the number of elements to be sorted.

    Note:
    If you follow my earlier answer, setting a breakpoint on cstring_cmp, you would quickly see when the compare method is called with invalid data, leading directly to the crash.

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

Sidebar

Ask A Question

Stats

  • Questions 166k
  • Answers 166k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found the solution. The button's parent was using the… May 12, 2026 at 1:25 pm
  • Editorial Team
    Editorial Team added an answer Your SocketChannel handling seems to be incomplete, see this complete… May 12, 2026 at 1:25 pm
  • Editorial Team
    Editorial Team added an answer No, it's not safe. It may work on your file… May 12, 2026 at 1:25 pm

Related Questions

Trying to follow this example. (Section String sorting...) Is there anything obvious that would
Is it a good idea to quote keys when using a hash in Perl?
We have a program that displays map data (think Google Maps, but with much
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
I am trying to follow this example: Creating Model Classes with the Entity Framework

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.