Search code examples
cstringmathsizemultiplication

Multiplying numbers as strings (corrupted size vs. prev_size)


I am doing a challenge in code war and I must multiply two numbers given as a string and return the result as a string. here you can find the challenge :

https://www.codewars.com/kata/multiplying-numbers-as-strings/train/c

so I've manged to pass all the Sample Tests that includes multiplying big numbers with 25+ digits as you can see in the site.

but when I click in Attempt button I get this error :

*** Error in `./test': corrupted size vs. prev_size: 0x0000000001ec9918 ***
======= Backtrace: =========   

you can copy my code below to see the full error text.

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

void zero(char *str,int len)   //this function fill my *str with 0s; 
{
  int i = 0;
  while (i <= len)
  {
        str[i] = '0';
        i++;
  }
}

//and this function below do multiplication that we learned when we were kids 
//you can do printf to see how this function work

char *multiply(char *a, char *b) {

  int l1 = strlen(a);
  int l2 = strlen(b);
  int index = l1 + l2;
  int new_i = index;
  int i = index;
  char *total = malloc(index);
  char *result = malloc(index);
  zero(total,index);
  int k = 0;
  int add = 0;
  int keep;

  while (l2 > 0)
  {
        l1 = strlen(a);
        k = 0;
        while (l1 > 0)
        {
              keep = total[i] - '0';
              total[i] = ((((total[i] - '0') + (( (b[l2 - 1] - '0') * (a[l1 - 1] - '0') + k ) % 10)) % 10) ) + '0';
              add =  ( ((keep) + (( (b[l2 - 1] - '0') * (a[l1 - 1] - '0') + k ) % 10)) / 10);
              k = (((b[l2 - 1] - '0') * (a[l1 - 1] - '0')) + k) / 10;
              if (k > 0 && l1 == 1)
                    total[i - 1] = k + '0'; 
              if (add > 0)
              {
                    if (total[i - 1] != '9')
                          total[i - 1] = ((total[i - 1] - '0') + add) + '0';
                    else
                    {
                          total[i - 1] = '0';
                          total[i - 2] = total[i - 2] + 1;
                    }
              }
              i--;
              l1--;
        }
        i = index - 1;
        index--;
        l2--;
  }

  i = 0;

  while (total[i] == '0')    //to avoid coping 0s into result
        i++;

  if (total[i] == '\0')      //in case of (0 * any positive number)
        i--;

  index = 0;      

  while (i <= new_i)
  {
        result[index] = total[i];
        i++;
        index++;
  }

  result[index] = '\0';

  return result;
}

I don't know where is the problem is it malloc or something else?


Solution

  • Your issue looks like an edge case and I am commenting on "Corrupted size..." rather than actual logic of multiplication.

    Within multiply function, I observed one issue. You are calculating an expected number of characters in final answer as "index = l1 + l2". However, when you are doing a "malloc", you must allocate "index + 1" bytes, so that you can store "\0" at the end even if your inputs generates largest possible answer.

    Best edge test case will be to use large number with "9999..." as both inputs.