2

i'm trying to make a function that draws a square, but i want to do it recursively, so i'm using a static variable, one problem: my function seg faults when I pass too high parameters to it, so i'm asking you if that is because of the static, or something else, thanks for your help, here's the code:

int     square(int x, int y)
{
    static t_coord  pos;

    if (x < 0 || y < 0)
    {
        ft_puts(2, "Error"); // if the size is negative it displays error on stderr
        return (FAILURE);
    }
    if (!pos.y)
        pos.y++;
    if (pos.x == x)
    {
        if (pos.y == y)
            return (SUCCESS);
        pos.y++;
        pos.x = 1;
        write(1, "\n", 1);
    }
    else
        pos.x++;
    ft_write_case(pos.x, pos.y, x, y); // function to write either corner side or space (middle)
    return(square(x, y));
}

the problem doesnt come from my ft_write_case function because i replaced it by a simple write(1, "o", 1) and it did the same segmentation fault, thanks for the help

Fayeure
  • 1,181
  • 9
  • 21
  • 2
    It's not the `static` variable. There's only one copy of the `static` variable. You're overflowing the stack with the `x` and `y` variables, and other behind-the-scenes stack usage. – user3386109 Apr 06 '20 at 21:15
  • 1
    When you call a function recursively, the recursive call should get you closer to the base case. You recurse with the same values of `x` and `y`, so it never stops recursing and you get a stack overflow. – Barmar Apr 06 '20 at 21:17
  • [Related](https://stackoverflow.com/q/3514283/509868) – anatolyg Apr 06 '20 at 21:19
  • 1
    @Barmar Although the `x` and `y` values don't change between calls, the `pos.x` and `pos.y` values do, so that should end the recursion. – Adrian Mole Apr 06 '20 at 21:19
  • Missed that return statement. – Barmar Apr 06 '20 at 21:20
  • 1
    Note that the function will only work the first time it's called, since `pos.x` and `pos.y` will stay at their final values, and there's no way to reset them. – user3386109 Apr 06 '20 at 21:21
  • There's a limit on the size of the stack, it might take too many recursions before `pos.x` and `pos.y` reach `x` and `y`. – Barmar Apr 06 '20 at 21:21
  • @user3386109 has a point. It would be better to pass `pos` as an argument, so you can call it with an initial value the first time. – Barmar Apr 06 '20 at 21:22
  • 2
    "_ i want to do it recursively, so i'm using a static variable,_" is a non-sequitur, one does not follow from the other. Recursion is generally ill-advised, and in this case probably not at all appropriate. – Clifford Apr 06 '20 at 21:27
  • 1
    @Clifford, Re, "recursion is generally ill-advised," That depends on what language you're using. In some programming languages, recursion (especially [_tail_ recursion](https://en.wikipedia.org/wiki/Tail_call)) is the _preferred_ way to express a computation. – Solomon Slow Apr 06 '20 at 21:33
  • ok so that means i'm overflowing the stack of my recursion? so if it's that even if I pass pos through arguments there will be as much recursions as there is now so this will not fix the issue – Fayeure Apr 06 '20 at 21:52
  • 1
    @SolomonSlow : I am not sure I agree, but this question is specifically about C code. There are certainly cases where recursion offers a simple and elegant solution, but you have to guarantee convergence in a deterministic number of recursions within the resource constraints of the environment. And you have to know what you are doing - which seems to be lacking here. – Clifford Apr 06 '20 at 22:02
  • So I added pos.x = 0 and pos.y = 0 before return(SUCCESS) for the multiple time call and i still have this seg fault issue – Fayeure Apr 06 '20 at 22:02
  • @Clifford yes in fact I just want to make the function as shorter as possible si i want to use recursion for that – Fayeure Apr 06 '20 at 22:04
  • @Fayeure What's the largest size that works? – user3386109 Apr 06 '20 at 22:04
  • @user3386109 since it has 2 values its hard to tell but I tested it for x = 100 and y = 100 and it worked, then x = 1000 and y = 1000 which did segmentation fault – Fayeure Apr 06 '20 at 22:06
  • The code already seems longer then necessary even for a non-recursive solution - I don't see how recursion is helping you here. – Clifford Apr 06 '20 at 22:07
  • @Clifford its just for optimisation: i'm trying to get the fastest way with less operations – Fayeure Apr 06 '20 at 22:10
  • 2
    For 1000x1000 the recursion depth is 1000000 and each recursion might use perhaps 16 bytes of stack on a 64bit system (the two int parameters, and the return address). So about 16Mb - that is more stack that the default process stack for Linux or Windows for example. – Clifford Apr 06 '20 at 22:11
  • It is not am optimisation in terms of memory use and how much faster is it? Have you measured it? All that memory has to get pushed and popped, I doubt it is any kind of optimisation. – Clifford Apr 06 '20 at 22:13

0 Answers0