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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:02:40+00:00 2026-06-11T05:02:40+00:00

I wrote a simple code for merge sort but it gave me this error:

  • 0

I wrote a simple code for merge sort but it gave me this error:

*** glibc detected *** ./merge: free(): invalid next size (fast): 0x09306058****Segmentation fault (core dumped)

This is the code:

#include<stdio.h>
#include<stdlib.h>

void mergesort(int[], int, int);
void merge(int[], int, int, int);
void printarray(int[]);

int gs;
int main()
{
    int* a,i,s;
    printf("Enter size of the array\n");
    scanf("%d",&s);
    a = (int*)calloc(s,sizeof(int));
    gs = s;
    printf("Enter the array\n");
    for(i=0;i<s;i++)
    {
        scanf("%d",&a[i]);
    }

    printf("Showing the array\n");

    printarray(a);
    printf("\n");

    mergesort(a,0,s-1);

    printf("The sorted array is:\n");
    printarray(a);

    free(a);
    return 0;
}

void mergesort(int a[], int f, int l)
{
    int m;
    if(f<l)
    {
        m = (f+l)/2;
        mergesort(a,f,m);
        mergesort(a,m+1,l);
        merge(a,f,m,l);
    }
}

void merge(int a[], int f, int m, int l)
{
    int* t,i,j,h1,h2;
    t = (int*)calloc(l-f+1,sizeof(int));
    i = h1 = f;
    h2 = m+1;
    while(h1 <= m && h2 <= l)
    {
        if(a[h1] < a[h2])
        {
            t[i] = a[h1];
            h1++;
        }
        else
        {
            t[i] = a[h2];
            h2++;
        }
        i++;
    }

    if(h1>m)
    {
        for(j=h2; j<=l; j++)
        {
            t[i] = a[j];
            i++;
        }
    }
    else
    {
        for(j=h1; j<=m; j++)
        {
            t[i] = a[j];
            i++;
        }
    }

    for(j=f;j<=l;j++)
    a[j] = t[j];

    free(t);
    printarray(a);
}

void printarray(int a[])
{
    int i;
    for(i=0;i<gs;i++)
    printf("%d ",a[i]);
    printf("\n");
}

I think the error is with the line free(t) in the function merge. For some reason that block is not being freed in the way it should be. How can I fix this?

  • 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-11T05:02:42+00:00Added an answer on June 11, 2026 at 5:02 am

    You are assigning out of bounds of t.

    int maxIdx = f+l-1;
    int * const t = (int*)calloc(maxIdx,sizeof(int));
    
    while(h1 <= m && h2 <= l) {
        assert(h1 < gs);
        assert(h2 < gs);
        assert(i < maxIdx);
        if(a[h1] < a[h2]) {
            t[i] = a[h1];
            h1++;
        } else {
            t[i] = a[h2];
            h2++;
        }
        i++;
    }
    

    When I run this:

    Assertion failed: (i < maxIdx), function merge, file crash.c, line 59.
    

    This is happening b/c l == 1 therefore i is 1 greater than the max allowed index.

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

Sidebar

Related Questions

I've wrote this simple code to attach events to one of my divs. But
I wrote this simple C code and compiled it using Visual Studio 2010, with
I've wrote this simple piece of code. And I have a slight problem with
i wrote the simple code ContentResolver contentResolver = this.getContentResolver(); Uri uriSMSURI = Uri.parse(content://sms/); //
For get device id in android sdk i wrote simple This code :: import
I wrote this simple code (in python) in test.py. I try to run timeit
I wrote simple code for deleting all entries from android calendar,but it didn't delete
I wrote very simple code to understand how columnstretch and calllater work but I
I've wrote a MIPS subroutine that implements the merge sort algorithm (the code is
I wrote a simple code to capture the netdevice notifications and simply print their

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.