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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:30:21+00:00 2026-06-07T05:30:21+00:00

I am beginner in C and this is my first program using malloc() function.

  • 0

I am beginner in C and this is my first program using malloc() function. I think there might be some problem in using this function.I wanted to use an array(cyclelength) where a solution for a range of number(user input) will be put. so the array size depends on user so i used malloc(). but the program is crashing. Here is my code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x,y,num,count,p,k;
    for(;;){
        printf("enter first integer. must be between 1 and 100000\n");
        scanf("%d", &x);
        printf("enter second integer. must be between 1 and 100000. must not equal the first integer.\n");
        scanf("%d", &y);
        if(x>=1 && x<100000 && y>=1 && y<100000 && x!=y){
            break;
        }
        else{
            printf("try the whole process again\n");
        }
    }
    if (x<y){
        int j;
        j=y;
        y=x;
        x=j;
    } //making x always greater than y
    int *cyclelength=malloc(5000*sizeof(int));
    if (NULL==cyclelength){
        printf("process aborted");
    }
    else{
        /*solution part for the range of number. and solution for each number  put into cyclelength.*/
        num=y;
        while(num<=x){
            p=1;
            k=num;
            while(k!=1){
                if(k%2==0)
                    k=k/2;
                else
                    k=3*k+1;
                p+=1;
                }
            count=0;
            cyclelength[count]=p;
            num+=1;
            count+=1;
        }
        free(cyclelength);
        cyclelength=NULL;
    }
    int c=0;
    int max=cyclelength[c];
    for(;c<x-y;c+=1){
        if(max<cyclelength[c+1]){
            max=cyclelength[c+1];
        }
    }
    printf("%d,%d,%d",x,y,max);
    return 0;
}
  • 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-07T05:30:22+00:00Added an answer on June 7, 2026 at 5:30 am

    You are using cyclelength after dealloating it so your program is crash.
    Try this :

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
        int x,y,num,count,p,k;
        for(;;){
            printf("enter first integer. must be between 1 and 100000\n");
            scanf("%d", &x);
            printf("enter second integer. must be between 1 and 100000. must not equal the first integer.\n");
            scanf("%d", &y);
            if(x>=1 && x<100000 && y>=1 && y<100000 && x!=y){
                break;
            }
            else{
                printf("try the whole process again\n");
            }
        }
        if (x<y){
            int j;
            j=y;
            y=x;
            x=j;
        } //making x always greater than y
        int *cyclelength=(int *)malloc(5000*sizeof(int));
        if (NULL==cyclelength){
            printf("process aborted");
        }
        else{
            /*solution part for the range of number. and solution for each number  put into cyclelength.*/
            num=y;
            while(num<=x){
                p=1;
                k=num;
                while(k!=1){
                    if(k%2==0)
                        k=k/2;
                    else
                        k=3*k+1;
                    p+=1;
                    }
                count=0;
                cyclelength[count]=p;
                num+=1;
                count+=1;
            }        
            // don't assign null to cyclelength
            //cyclelength=NULL;
        }
        int c=0;
        int max=cyclelength[c];
        for(;c<x-y;c+=1){
            if(max<cyclelength[c+1]){
                max=cyclelength[c+1];
            }
        }
        printf("%d,%d,%d",x,y,max);
        // free here
        free(cyclelength);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time here and I am a beginner so kindly bear
First of all, apologies if this too naive (I am a beginner). I have
I'm getting a weird problem in GlassFish 2.1 - I'm a beginner with this
This is the first time I've used pthreads. I'm having trouble because some times
Beginner's question: I dared to introduce constants into my little program. I first resisted,
hi all this is my first question. I am beginner with web apps totally.
I am a beginner at Python, and this is my first post, so don't
This is my first C++ program. It prints the number of words in the
To start, let me say that I'm a beginner and this program is my
I'm a beginner programmer and I have this problem in C#. The solution 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.