Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Do this program have the specified behavior?

47 views
Skip to first unread message

olcott

unread,
Nov 16, 2021, 10:33:49 PM11/16/21
to
#include <stdint.h>
typedef void (*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.


--
Copyright 2021 Pete Olcott

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

wij

unread,
Nov 16, 2021, 11:10:42 PM11/16/21
to
I would guess yes, it is specified: BAD, illegal program

[]$ g++ t.cpp
t.cpp: In function ‘int H(ptr, ptr)’:
t.cpp:6:2: error: too many arguments to function
6 | x(y); // direct execution of P(P)
| ~^~~
t.cpp: In function ‘int main()’:
t.cpp:18:3: error: invalid conversion from ‘int (*)(ptr)’ {aka ‘int (*)(void (*)())’} to ‘ptr’ {aka ‘void (*)()’} [-fpermissive]
18 | H(P, P);
| ^
| |
| int (*)(ptr) {aka int (*)(void (*)())}
t.cpp:4:11: note: initializing argument 1 of ‘int H(ptr, ptr)’
4 | int H(ptr x, ptr y)
| ~~~~^
t.cpp:18:6: error: invalid conversion from ‘int (*)(ptr)’ {aka ‘int (*)(void (*)())’} to ‘ptr’ {aka ‘void (*)()’} [-fpermissive]
18 | H(P, P);
| ^
| |
| int (*)(ptr) {aka int (*)(void (*)())}
t.cpp:4:18: note: initializing argument 2 of ‘int H(ptr, ptr)’
4 | int H(ptr x, ptr y)
| ~~~~^

olcott

unread,
Nov 16, 2021, 11:46:52 PM11/16/21
to
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++

Chris M. Thomasson

unread,
Nov 17, 2021, 12:13:12 AM11/17/21
to
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...

Richard Damon

unread,
Nov 17, 2021, 6:53:56 AM11/17/21
to
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.



olcott

unread,
Nov 17, 2021, 9:33:53 AM11/17/21
to
On 11/17/2021 2:28 AM, Mark Bluemel wrote:
> 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.

olcott

unread,
Nov 17, 2021, 9:49:42 AM11/17/21
to
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

Anurag Ranjan

unread,
Nov 17, 2021, 5:19:37 PM11/17/21
to
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;
}


Juha Nieminen

unread,
Nov 18, 2021, 1:33:17 AM11/18/21
to
In comp.lang.c++ olcott <No...@nowhere.com> wrote:
>>> typedef int (*ptr)();
>>>
>>> int H(ptr x, ptr y)
>>> {
>>> x(y); // direct execution of P(P)
>>> return 1;
>>> }
>
> This code does compile and run correctly.

It might compile as C, but it shouldn't compile as C++, because it's trying
to call an int(*)() with a parameter, even though that function (that the
pointer is pointing to does not take any parameter.)

Perhaps if it were an int(*)(void*) and then you reinterpret-cast the
parameter in order to call it, then it would compile.

As for "runs correctly", I don't see how this "runs correctly". Unless you
mean an infinite recursion that does nothing "runs correctly".

Ben Bacarisse

unread,
Nov 18, 2021, 6:16:01 AM11/18/21
to
Juha Nieminen <nos...@thanks.invalid> writes:

> In comp.lang.c++ olcott <No...@nowhere.com> wrote:
>>>> typedef int (*ptr)();
>>>>
>>>> int H(ptr x, ptr y)
>>>> {
>>>> x(y); // direct execution of P(P)
>>>> return 1;
>>>> }
>>
>> This code does compile and run correctly.
>
> It might compile as C, but it shouldn't compile as C++, because it's trying
> to call an int(*)() with a parameter, even though that function (that the
> pointer is pointing to does not take any parameter.)

Just as it must not compile in C++, this code must compile in C. The
meaning of empty ()s in a function declaration is different between the
two languages.

When I tidied up the code, I wrote C, not C++, because C has a more
suitable type available.

--
Ben.

olcott

unread,
Nov 18, 2021, 9:58:38 AM11/18/21
to
This is the question that I am asking:

Does this program have the specified behavior?
For every H that simulates or executes its input and aborts or does not
abort its input P never reaches its last instruction.

olcott

unread,
Nov 18, 2021, 10:00:45 AM11/18/21
to
You did a really great job.
0 new messages