- Do this program have the specified behavior? - 8 Updates
| olcott <NoOne@NoWhere.com>: Nov 16 09:33PM -0600 #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 <wyniijj@gmail.com>: Nov 16 08:10PM -0800 On Wednesday, 17 November 2021 at 11:33:49 UTC+8, olcott wrote: > Talent hits a target no one else can hit; > Genius hits a target no one else can see. > Arthur Schopenhauer 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 <NoOne@NoWhere.com>: Nov 16 10:46PM -0600 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++ -- Copyright 2021 Pete Olcott Talent hits a target no one else can hit; Genius hits a target no one else can see. Arthur Schopenhauer |
| "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Nov 16 09:12PM -0800 On 11/16/2021 8:46 PM, olcott wrote: > { > H(P, P); > } Stack overflow... |
| Richard Damon <Richard@Damon-Family.org>: Nov 17 06:53AM -0500 On 11/16/21 11:46 PM, olcott wrote: >> 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 <NoOne@NoWhere.com>: Nov 17 08:33AM -0600 On 11/17/2021 2:28 AM, Mark Bluemel wrote: >>> } >> 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? -- Copyright 2021 Pete Olcott Talent hits a target no one else can hit; Genius hits a target no one else can see. Arthur Schopenhauer |
| olcott <NoOne@NoWhere.com>: Nov 17 08:49AM -0600 On 11/17/2021 5:53 AM, Richard Damon wrote: > 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 |
| Anurag Ranjan <anuragranjan630@gmail.com>: Nov 17 10:00PM 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; } |
| You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment