Hi guys I'm new to Stackoverflow so this is my first question. Also new to C. So I'm writing a simple program and the code that I'm having problems with does the following: Takes an int array and multiplies every other element by 2 starting from the second last and puts the result of every multiplication into a new array. Since the original array is based on user input, I want the new array to have dynamic size, and allocate the size every time new elements are added into the array.
The problem
The program works well with a small array (up to 10 elements or so), going bigger than that gives me the following error:
realloc(): invalid next size Aborted
I really need help since I've searched for a solution a lot but no answer seems to help. Thanks in advance!
My code:
int checksum(int *arr, int length)
{
int *arr1=NULL;
for(int i = 0, j = 1, k = length-2; i < j; i++, k-=2)
{
    if(k >= 0)
    {
        if(i == 0)
        {
            arr1 = (int*)malloc(sizeof(int));
        }
        else
        {
            do
            {
                arr1 = (int*)realloc(arr1, i+1 * sizeof(int));
            }
            while (arr1 == NULL);
        }
        int n = arr[k]*2;
        if(n/10 == 0)
        {
            arr1[i] = n;
            printf("%d", arr1[i]);
            j++;
        }
        else
        {
            do
            {
            arr1 = (int*)realloc(arr1, i+2 * sizeof(int));
            }
            while (arr1 == NULL);
            arr1[i] = n / 10;
            arr1[i+1] = n % 10;
            printf("%d%d", arr1[i], arr1[i+1]);
            i++;
            j+=2;
        }
    }
  }
}
