I'm getting SIGABRT error. I think there is a buffer overflow but I couldn't detect

159 Views Asked by At

I have created a C code that print all different combinations for given n with ascending order. Code works correctly for all potential inputs in my compiled file. But I'm getting the SIGABRT error. Also, what is exactly SIGABRT error in C?

#include <unistd.h>

void    ft_putchar(char c)
{
    write(1, &c, 1);
}

void    ft_print_digits(int n, int *digits)
{
    int counter;

    counter = 0;
    while (counter < n)
    {
        ft_putchar(digits[counter] + 48);
        counter = counter + 1;
    }
    if (digits[0] != (10 - n))
    {
        ft_putchar(',');
        ft_putchar(' ');
    }
}

void    ft_inc(int n, int *digits, int *pCount)
{
    int index;

    index = *pCount;
    while (index < n - 1)
    {
        digits[index + 1] = digits[index] + 1;
        index = index + 1;
    }
    if (index == n - 1)
    {
        *pCount = index + 1;
    }
    else
    {
        *pCount = index;
    }
    ft_print_digits(n, digits);
}

void    ft_print_combn(int n)
{
    int digits[12];
    int counter;
    int max_digit;

    counter = 0;
    while (counter < n)
    {
        digits[counter] = counter;
        counter = counter + 1;
    }
    ft_print_digits(n, digits);
    counter = n - 1;
    while (counter >= 0)
    {
        max_digit = 10 - n + counter;
        if (digits[counter] < max_digit)
        {
            digits[counter]++;
            ft_inc(n, digits, &counter);
        }
        counter = counter - 1;
    }
}

int main()
{
    ft_print_combn(3);
    return(0);
}

I've compiled with gcc -Wall -Werror -Wextra ft_print_combn.c And 0 < n < 10. So there must be a SIGABRT error in the code but I couldn't find it.

0

There are 0 best solutions below