0

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.

  • Well, how do you call `ft_print_combn()`? – 500 - Internal Server Error Feb 04 '23 at 18:10
  • 1
    Where's the `main()` function? Kindly provide a minimal reproducible example and the verbatim error log. I have never came across any `SIGAPORT` errno code or signal. Is it `SIGABRT` or `SIGAPORT`? – Harith Feb 04 '23 at 18:23
  • I added the main function sorry about that. And it's SIGABRT. I don't get any error when I compile but when I upload the code to the moulinette it says there is a SIGABRT error. Also, what is verbatim? – Huseyin Donmez Feb 04 '23 at 18:53
  • 1
    main calls `ft_print_number()` but there's no such function. – pmacfarlane Feb 04 '23 at 18:56
  • @HuseyinDonmez If that comment was directed at me, then you need to mention a tag by specifying the account name after the @ symbol. Otherwise, I won't be notified. As of verbatim, it means: *"in exactly the same words as were used originally."* Your code is still not reproducible. As already pointed out in the comments, `main()` calls `ft_print_number()`, but there's no such function defined in your code or prototyped in `unistd.h`. So I find it unlikely that your code was able to compile successfully as originally claimed. – Harith Feb 04 '23 at 19:00
  • As of `SIGABRT`, it's a signal, not an error in your code, Though it might be caused due to an error in your code. See https://stackoverflow.com/q/3413166/20017547 – Harith Feb 04 '23 at 19:06
  • I speculated that you meant to call `ft_print_combn()`. It compiles without error for me and seems to run ok. I don't know if the output is correct, but there is no signal generated. Does the code as it is posted now still generate a SIGABRT for you? Please copy and paste it into a new file and test it. – pmacfarlane Feb 04 '23 at 19:37
  • @pmacfarlane yes I meant to call I've just fixed the typo – Huseyin Donmez Feb 04 '23 at 19:38
  • @Haris firstly, thank you for this explanation. I meant to call `ft_print_combn()`. Also, thanks for the link – Huseyin Donmez Feb 04 '23 at 19:40
  • I compiled your code and was unable to reproduce the problem. Is this the same code that caused that signal? – Harith Feb 04 '23 at 19:56

0 Answers0