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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:40:31+00:00 2026-06-04T21:40:31+00:00

I am making a big integer multiplier in C without any other sources such

  • 0

I am making a big integer multiplier in C without any other sources such as GMP. It can multiply a 10,000 digit integer by another. The problem I am having is after a certain amount of multiplications (for me its after about 3 large multiplications or 25 small and 3 large) My program freezes. It uses about 1.8 GB of RAM and then the stops. I’m not quite sure what is causing this, I know that different numbers have the same result.
Thanks for any help.

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

    #define MAX 10001

    struct integer {
    int* digits;
    int size;
    };

    struct integer* add(struct integer* one, struct integer *two);
    void print(struct integer* number);
    void print_op(struct integer* op1, struct integer* op2,struct integer* tempwer,char op);
    struct integer* convert_integer(char* word);
    void free_struct(struct integer* thisint);
    struct integer* multiply(struct integer *p, struct integer *q);


    int main() {

    FILE* ifp = fopen("bigint.txt", "r");

    int loop, numcases;
    fscanf(ifp, "%d", &numcases);

    // Go through each case.
    for (loop=1; loop<=numcases; loop++) {

        // Read in both operands.
        char op1[MAX];
        char op2[MAX];
        fscanf(ifp, "%s%s", op1, op2);

        // Convert and compute.
        struct integer* first = convert_integer(op1);
        struct integer* second = convert_integer(op2);
        struct integer* ans = multiply(first, second);


        printf("Problem #%d: ", loop);
        print_op(first, second, ans, '*');
        printf("\n");

        // After we output, we don't need these any more.
        free_struct(first);
        free_struct(second);
        free_struct(ans);


    }

    return 0;
}

// Pre-conditions: Both one and two are not NULL pointers that point to
//                 linked lists structures that store non-negative digits
//                 at each node.
// Post-conditions: A pointer to a linked list will be returned. The
//                  linked list will store the value of the sum of the
//                  two integers represented by the linked lists passed
//                  into the function.
struct integer* add(struct integer* one, struct integer *two) {

    struct integer *ans;
    int digit1 = 0, digit2 = 0, carry = 0, result, i;


    ans = (struct integer *)malloc(sizeof(struct integer));

    //allocate space for the larger of the 2 arrays
    if(one->size>two->size)
        ans->size=one->size;
    else
        ans->size=two->size;

    ans->digits=(int*)(malloc(sizeof(int)*ans->size));

    for(i=0;i<ans->size;i++){
        // Determine digit to add from first operand.
        if (i<one -> size)
            digit1 = one -> digits[i];
        else
            digit1 = 0;

        // Determine digit to add from second operand.
        if (i<two -> size)
            digit2 = two -> digits[i];
        else
            digit2 = 0;

        // Compute correct addition digit and carry.
        result = (digit1 + digit2 + carry)%10;
        carry = (digit1 + digit2 + carry)/10;

        // Store result in the appropriate linked list.
        ans -> digits[i] = result;

    }//for

    // Take care of the most significant digit if there is a carry.
    if (carry != 0) {

        //copy off the whole array into a new one
        //of size+1 and free the old one in case of carry
        ans->size+=1;
        ans->digits = (int *)realloc(ans->digits, sizeof(int)*ans->size);
        ans->digits[ans->size-1] = carry;
    }

    // Return the ptr. to the added result.
    return ans;
}

// Precondition: number points to a not NULL linked list that contains
//               only single digits stored at each node.
// Postcondition: The integer represented by the linked list pointed to by
//                number will be printed out.
void print(struct integer* number) {

    int i;
    if (number != NULL) {

        // Loop through in backwards order, since number is stored reversed.
        for(i=number->size-1;i>=0;i--){
            printf("%d",number->digits[i]);
        }
    }

}

// Precondition: op1 and op2 point to valid linked lists storing integers,
//               operator is a valid integer operator, and tempwer is the
//               value of applying the first operation to the second.
// Postcondition: The arithmetic operation desired (op1 operator op2) will
//                be printed to the screen in a reasonable manner.
void print_op(struct integer* op1, struct integer* op2,struct integer* tempwer,char op) {

    print(op1);
    printf(" %c ", op);
    print(op2);
    printf(" = ");
    print(tempwer);


}

//Preconditions: the first parameter is a pointer to a
//  pointer variable that points to
//  struct integer. The function skips leading
//  blanks and assumes that no leading zeros are
//  entered at the input.
//Postconditions: The function will read the digits of the
//  large integer character by character,
//  convert them into integers and place them in
//  nodes of a linked list. The pointer to the
//  head of the list is returned as the value of
//  the input parameter.
struct integer* convert_integer(char* word) {

    int i;

    struct integer *ans=(struct integer *)(malloc(sizeof(struct integer)));
    ans->size=0;
    if(word==NULL) ans->digits=NULL;

    else {

        // Allocate space for each of the digits.
        ans->size = strlen(word);
        ans->digits=(int *)(malloc(sizeof(int)*ans->size));

        // Store them in reverse order.
        for(i=0;i< ans->size;i++)
            ans->digits[ans->size-i-1]=word[i] - '0';

    }//if word not NULL

    return ans;
}

//Preconditions: p and q are pointers to struct integers.
//Postconditions: A new struct integer is created that
//                stores the product of the integers
//                pointed to by p and q and a pointer to it
//                is returned.
struct integer* multiply(struct integer *p, struct integer *q){

    struct integer *temp;
    struct integer *ans;

    int digit1 = 0, digit2 = 0, carry = 0, index=0, front=0, result, i, j, pos, preSize;

    temp = (struct integer *)calloc(sizeof(struct integer),1);
    ans = (struct integer *)calloc(sizeof(struct integer),1);


    //allocate space for the larger of the 2 arrays
    //Which ever array is larger will be the starting size of the tempwer array.
    if(q->size>p->size)
        temp->size = q->size;

    else
        temp->size = p->size;


    temp->digits=(int*)(calloc(sizeof(int)*temp->size,1));


    //use a double for loop, one for the top number and one for bottom.
    for(i=0; i<q->size; i++){

        //Make the starting size the size of the biggest number.
        if(q->size>p->size)
            temp->size = q->size;

        else
            temp->size = p->size;


        temp->digits=(int*)(calloc(sizeof(int)*temp->size,1));

        if (i < q->size)
            digit1 = q->digits[i];
        else
            digit1 = 0;




        //Bottom part of the multiplication.
        for(j=0; j<p->size; j++){

            // Determine digit to add from first operand.
            if (j < p->size)
                digit2 = p->digits[j];
            else
                digit2 = 0;

            // Compute correct multiplication digit and carry.
            //gives last digit
            result = (digit1 * digit2 + carry)%10;
            //drops last digit
            carry = (digit1 * digit2 + carry)/10;

            // Store result in the appropriate linked list.
            temp -> digits[j] = result;

        }

        //Add a zero to the end of the next number (like multiplying by hand).
        if(i>0){

            temp->size += i;
            temp->digits = (int *)(realloc(temp->digits, sizeof(int)*temp->size));


            for(j=temp->size; j>0; j--){

                if((j-1-i)<0)
                    break;
                //Shift everthing over by 1.
                else
                    temp->digits[j-1]=temp->digits[j-1-i];

            }
            //Make the new number zero.
            for(j=1; j<=i; j++)
                temp -> digits[j-1] = 0;

        }//if


        //If there is a carry insert it in front of the number.
        if (carry != 0) {

            temp->size += 1;
            temp->digits = (int *)(realloc(temp->digits, sizeof(int)*temp->size));
            temp->digits[temp->size-1]=0;

            //Find the front of the number.
            for(j=temp->size; j>0; j--){

                if(temp->digits[j-1] != 0){

                    front = j;
                    break;
                }
            }

            //Insert it.
            if(result == 0)
                temp->digits[front+1] = carry;
            else
                temp->digits[front] = carry;

            carry = 0;

        }//if




    //Delete any leading zeros.
    if(temp->size != 1){

        for(j=temp->size; j>0; j--){

            //Finds where the zeros end.
            if(temp->digits[j-1] != 0){
                pos =j;
                break;
            }

            //Counts the zeros.
            if(temp->digits[j-1] == 0)
                index++;

            }

        //If they are all zeros then make the temp number 0 of size 1.
        if(index==temp->size) {
            pos=1;
            temp->size = temp->size-(temp->size-pos);
            temp->digits = (int *)realloc(temp->digits, sizeof(int)*temp->size);
        }
        //If not then reallocate with the extra zeros removed.
        else{
            temp->size = temp->size-(temp->size-pos);
            temp->digits = (int *)realloc(temp->digits, sizeof(int)*temp->size);
        }

        index=0;

    }//if


        //If this is the first time saving the answer the allocate memory for it.
        if(i==0){
            ans->size = temp->size;
            ans->digits=(int*)(calloc(sizeof(int)*temp->size,1));
        }

        //Add the temp number to the total (ans).
        ans = add(temp, ans);

        //Clear temp.
        for(j=0; j<temp->size; j++)
            temp->digits[j]=0;


    }//for on top


    free_struct(temp);

    return ans;

}


    // Frees the memory for the struct pointed to by thisint.
    void free_struct(struct integer* thisint) {
    free(thisint->digits);
    free(thisint);
}
  • 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-04T21:40:32+00:00Added an answer on June 4, 2026 at 9:40 pm

    In this part of your multiply method:

        temp->digits=(int*)(calloc(sizeof(int)*temp->size,1));// <-- allocate some
    
        //use a double for loop, one for the top number and one for bottom.
        for(i=0; i<q->size; i++){
    
            //Make the starting size the size of the biggest number.
            if(q->size>p->size)
                temp->size = q->size;
            else
                temp->size = p->size;
    
            temp->digits=(int*)(calloc(sizeof(int)*temp->size,1)); // <--allocate again
    

    It looks like you are allocating memory without matching frees other than the last one that gets free’d at the end of the function. It’s been many years since I used c, so hopefully I haven’t missed something obvious.

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

Sidebar

Related Questions

Making websites that appear correctly in IE is a big problem. Is there any
I'm trying to maniplulate a string without making a big issue out of it
Yet another C#/.NET guy trying to learn Objective-C with big dreams of making millions
I'm making a program that takes a three-digit integer and splits it into two
I have a very big problem. I am making a CRM (Costumer Relationship Management)
I'm making a program that asks for an integer from the user, it then
I'm using this Big Integer library for Javascript: http://www.leemon.com/crypto/BigInt.js and I need to be
Okay, im making a pretty big file in my opinion, so i wanted to
I'm making a C program that communicates with a patient monitor that uses big
I'm making a big C project and I have never come across a situation

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.