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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:44+00:00 2026-05-26T22:57:44+00:00

I tried executing this code in gcc as well as turboc. In GCC it

  • 0

I tried executing this code in gcc as well as turboc. In GCC it gives me a segmentation fault error at run time and in turbo it gives a null pointer assignment error again at run time.

I tried tracing it but i cant see where the problem is.

#include<stdio.h>

int heapsize;

void heapify(int a[],int i)
{
    int l=2*i,r=2*i+1,largest,temp;
    if(l<=heapsize && a[l]>a[i])
        largest=l;
    else
        largest=i;
    if(r<=heapsize && a[r]>a[largest])
        largest=r;
    if(largest!=i)
    {
        temp=a[i];
        a[i]=a[largest];
        a[largest]=temp;
        heapify(a,largest);
    }
}

void buildheap(int a[],int length)
{
    int i;  
    heapsize=length;    
    for(i=length/2;i>=0;i--)
        heapify(a,i);
}

void heapsort(int a[],int length)
{
    int i,temp; 
    buildheap(a,length);
    for(i=length-1;i>=1;i++)
    {
        temp=a[0];
        a[0]=a[i];
        a[i]=temp;
        heapsize--;         
        heapify(a,0);   
    }
}

void main()
{
    int a[20],i,length;
    printf("ENTER THE SIZE OF YOUR ARRAY:");
    scanf("%d",&length);
    printf("ENTER THE ARRAY ELEMENTS: \n");
    for(i=0;i<length;i++)
        scanf("%d",&a[i]);
    heapsort(a,length);
    printf("THE SORTED ARRAY IS:");
    for(i=0;i<length;i++)
        printf("%d /t",a[i]);
}

NOTE: I coded this using the heapsort algorithm given in CLRS.

Edit: here’s the input i gave and the error I got.

chaitanya@chaitanya-laptop:~/Desktop/My prog$ ./a.out
ENTER THE SIZE OF YOUR ARRAY:5
ENTER THE ARRAY ELEMENTS:
9
5
8
7
6
Segmentation fault

Edit: apparently a silly mistake of i++ instead of i– was causing the problem. but now there seems to be a logical error since the program is not giving the sorted array as the output.

ENTER THE SIZE OF YOUR ARRAY:5
ENTER THE ARRAY ELEMENTS:
2
4
3
1
9
THE SORTED ARRAY IS:3013077 4 1 9 2

  • 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-26T22:57:45+00:00Added an answer on May 26, 2026 at 10:57 pm

    The following for loop will never terminate. The index keeps on increasing causing array to go out of bounds and that’s the reason you get the above error.

    for(i=length-1;i>=1;i++)
    {
        temp=a[0];
        a[0]=a[i];
        a[i]=temp;
        heapsize--;         
        heapify(a,0);   
    }
    

    Update : Your heapify() is incorrect.. try this instead

    if(l<heapsize && a[l]>a[i])
        largest=l;
    else
        largest=i;
    if(r<heapsize && a[r]>a[largest])
        largest=r;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this code in visual basic. On executing no error is printed
I want to delay blocking by 2 seconds while executing this code. How can
I have tried many ways of executing this but its not happening .. here
I tried installing Hadoop following this http://hadoop.apache.org/common/docs/stable/single_node_setup.html document. When I tried executing this bin/hadoop
I've tried executing the following: #!C:\cygwin\bin\bash.exe ls ${WORKSPACE} But that doesn't find ls (even
Below is the command I tried executing, without success: exec('ln -s ' . PLUGIN_DIR
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
I have this code to load a config file and read all the values
I have this code: $('#label, #btlabel').live('hover',function(){ parent = $(this).parent().attr('id'); position = $(this).parent().position(); positionTop =

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.