Thread: Why does this string multiplier code crash after about 200 characters?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    12

    Why does this string multiplier code crash after about 200 characters?

    effective on small numbers.

    Code:
    #include<stdio.h>
    //#include<math.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    int MAX=1000;
    
    
    char * multiply(char [],char[]);
    int main(){
        char a[MAX];
        char b[MAX];
        char *c;
        int la,lb;
        int i;
        printf("Enter the first number : ");
        scanf("%s",a);
        printf("Enter the second number : ");
        scanf("%s",b);
        printf("Multiplication of two numbers : ");
        c = multiply(a,b);
        printf("%s",c);
        return 0;
    }
    
    
    char * multiply(char a[],char b[]){
        char mul[MAX];
        char c[MAX];
        char temp[MAX];
        int la,lb;
        int i,j,k=0,x=0,y;
        long int r=0;
        long sum = 0;
        la=strlen(a)-1;
            lb=strlen(b)-1;
       
            for(i=0;i<=la;i++){
                    a[i] = a[i] - 48;
            }
    
    
            for(i=0;i<=lb;i++){
                    b[i] = b[i] - 48;
            }
    
    
        for(i=lb;i>=0;i--){
             r=0;
             for(j=la;j>=0;j--){
                 temp[k++] = (b[i]*a[j] + r)%10;
                 r = (b[i]*a[j]+r)/10;
             }
             temp[k++] = r;
             x++;
             for(y = 0;y<x;y++){
                 temp[k++] = 0;
             }
        }
       
        k=0;
        r=0;
        for(i=0;i<la+lb+2;i++){
             sum =0;
             y=0;
             for(j=1;j<=lb+1;j++){
                 if(i <= la+j){
                     sum = sum + temp[y+i];
                 }
                 y += j + la + 1;
             }
             c[k++] = (sum+r) %10;
             r = (sum+r)/10;
        }
        c[k] = r;
        j=0;
        for(i=k-1;i>=0;i--){
             mul[j++]=c[i] + 48;
        }
        mul[j]='\0';
        return mul;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    Did you try to run the program with your debugger? The debugger should be able to tell you exactly where the problem is detected.


    By the way returning a pointer to a local variable may be part of the problem.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,187
    For a quick fix to returning a local you could make mul static.
    Better still, return a malloc'ed string.
    MAX should be a define.

    In your for (i = lb; i >= 0; i--) loop, k is going way out of bounds.
    So it's not working correctly for small numbers, either, even though in that case it's not causing a segfault.
    For example, when multiplying 1234567890 by 1234567890, k reaches 165 although the result is only 19 digits long.
    We live as it were by chance, and by chance we are governed. - Seneca

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    12
    The entire point of adding strings is to be able to multiply large numbers.
    This method has multiple flaws because it integrates using integers at the wrong places.

    I would recommend not using this code.

    The author writes the numbers code backwards in temp[k++] and then leaves sum and r as long int. This complicates the entire point of using strings.


    I hate freemasonry.
    Last edited by cprogramnoob; 4 Weeks Ago at 04:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2016, 02:59 AM
  2. Replies: 4
    Last Post: 11-12-2015, 02:49 PM
  3. crash my code.
    By caroundw5h in forum C Programming
    Replies: 7
    Last Post: 11-06-2005, 12:29 PM
  4. Why this code crash ?
    By MaaSTaaR in forum C Programming
    Replies: 4
    Last Post: 08-22-2005, 10:50 AM
  5. Code Crash!!!
    By John Bosko in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2002, 06:24 AM

Tags for this Thread