• Re: Does this program have the specified behavior?

    From olcott@21:1/5 to Mark Bluemel on Wed Nov 17 08:33:29 2021
    XPost: comp.lang.c

    On 11/17/2021 2:28 AM, Mark Bluemel wrote:
    On Wednesday, 17 November 2021 at 05:13:08 UTC, Chris M. Thomasson wrote:
    On 11/16/2021 8:46 PM, olcott wrote:
    #include <stdint.h>
    #include <stdio.h>
    typedef int (*ptr)();

    int H(ptr x, ptr y)
    {
    x(y); // direct execution of P(P)
    return 1;
    }

    int P(ptr x)
    {
    H(x, x);
    return 1;
    }

    int main(void)
    {
    H(P, P);
    }
    Stack overflow...

    Surely a compiler can optimise this to remove the recursion, and for that matter, remove any functionality at all, as the code has no observable behaviour, as far as I can see (I'd be happy to be corrected on this!).


    This is the question:
    For every H that simulates or executes its input and aborts or does not
    abort its input does any P ever reach its last instruction?


    $DIETY only knows what olcott was trying to show with this code.





    --
    Copyright 2021 Pete Olcott

    Talent hits a target no one else can hit;
    Genius hits a target no one else can see.
    Arthur Schopenhauer

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From olcott@21:1/5 to Richard Damon on Wed Nov 17 08:49:16 2021
    XPost: comp.lang.c

    On 11/17/2021 5:53 AM, Richard Damon wrote:
    On 11/16/21 11:46 PM, olcott wrote:
    On 11/16/2021 10:10 PM, wij wrote:
    On Wednesday, 17 November 2021 at 11:33:49 UTC+8, olcott wrote:

    #include <stdint.h>
    #include <stdio.h>
    typedef int (*ptr)();

    int H(ptr x, ptr y)
    {
       x(y); // direct execution of P(P)
       return 1;
    }

    int P(ptr x)
    {
       H(x, x);
       return 1;
    }

    int main(void)
    {
       H(P, P);
    }

    For every H that simulates or executes its input and aborts or does not
    abort its input P never reaches its last instruction.

    I would guess yes, it is specified: BAD, illegal program

    It must be compiled as C and the typedef was incorrect.
    I can't find a way to specify the "C" calling convention for g++


    Shows you don't understand C++.

    Making it 'C' calling convention doesn't change the syntax for the call.

    In C++, an empty parameter list specifies a void parameter list, while
    in C it specifies tha mostly obsolete concept that this is a
    non-prototype declaration that doesn't specify the parameters.

    Ultimately the problem is that this runs into the issue that it is
    impossible to express in C a function that takes or returns a pointer to
    the same type as the function itself is, as that requires self
    references in the type definition.


    This code does compile and run correctly.
    The only difference from Ben's code is that
    void (*ptr)(); was changed to int (*ptr)();

    In comp.lang.c On 11/11/2021 2:31 PM, Ben Bacarisse:
    transformed my syntax into his syntax




    --
    Copyright 2021 Pete Olcott

    Talent hits a target no one else can hit;
    Genius hits a target no one else can see.
    Arthur Schopenhauer

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anurag Ranjan@21:1/5 to Wolcott on Wed Nov 17 22:00:00 2021
    XPost: comp.lang.c

    On 17/11/2021 04:46, Wolcott wrote:
    On 11/16/2021 10:10 PM, wij wrote:
    On Wednesday, 17 November 2021 at 11:33:49 UTC+8, olcott wrote:




    #include <stdio.h>

    void count(int n)
    {
    static int d = 1;
    printf("n = %d\n", n);
    printf("d = %d\n", d);

    d++;

    if (n > 1)
    {
    count(n - 1);
    }
    printf("d = %d\n", d);
    }
    int main()
    {
    count(5);
    return 0;
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)