-1

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;
        }
    }
  }
}
Devplayer
  • 21
  • 5
  • 2
    Welcome to the site. Questions asking for debugging help should include a [mcve]. Your code is not quite that - it is not a complete program that someone could compile and test. It needs a main function, includes, and so on. – Nate Eldredge Nov 29 '20 at 01:22
  • 5
    Should `i+1 * sizeof(int)` be `(i+1) * sizeof(int)`? – Fiddling Bits Nov 29 '20 at 01:22
  • 2
    Some general comments: [Casting the result of `malloc` is not recommended](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). And your `do { arr1 = realloc(...) } while (arr1 == NULL)` is not really a good idea; in the vast majority of situations, if memory allocation fails it is not likely to succeed anytime soon. So now your program is still not working and it's spinning at 100% CPU instead of aborting with a useful message. – Nate Eldredge Nov 29 '20 at 01:25
  • 1
    @Fiddling Bits Yes, I think that might be the issue :)). I will test soon. – Devplayer Nov 29 '20 at 01:25
  • @Nate Eldredge Sorry I didn't realize, I will edit ASAP. Also thanks for the tips, really useful!! But care to expand on why casting the result of malloc is not a good idea? Since that's how I see it being used everywhere(not casting would return void AFAIK). – Devplayer Nov 29 '20 at 01:28
  • The error message (invalid next size) means you've scribbled out of bounds of the allocated memory. If it is available for your platform, use [Valgrind](https://valgrind.org/) to find where you run into trouble. But the incorrect size for the allocation is a good place to start, Valgrind or no Valgrind. – Jonathan Leffler Nov 29 '20 at 01:37
  • @Jonathan Leffler Thanks a lot, very useful!! – Devplayer Nov 29 '20 at 01:59
  • Use [GCC](http://gcc.gnu.org/) and compile with all warnings and debug info: `gcc -Wall -Wextra -g`. Then use [valgrind](http://valgrind.org/) and [GDB](https://www.gnu.org/software/gdb/) – Basile Starynkevitch Nov 29 '20 at 02:26

1 Answers1

2

Should i+1 * sizeof(int) be (i+1) * sizeof(int)? – Fiddling Bits

Thanks @Fiddling Bits for the answer, it turns out I missed a parenthesis there, so so it was not allocating the proper size for the array. Also thanks to all for the tips!! I will mark as solved as soon as I'm allowed to(2 days apparently, I guess since I'm a new user).

Devplayer
  • 21
  • 5
  • 2
    Good job at pressing though until you have found an answer. When you have solved your problem, there is no need to edit your question to mark it solved, your answer will take care of it. – David C. Rankin Nov 29 '20 at 02:10