Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #92398 > unrolled thread

“ChattyG takes a college freshman C/C++ programming exam”

Started byLynn McGuire <lynnmcguire5@gmail.com>
First post2023-10-05 17:40 -0500
Last post2023-10-23 16:45 +0300
Articles 20 on this page of 57 — 18 participants

Back to article view | Back to comp.lang.c++


Contents

  “ChattyG takes a college freshman C/C++ programming exam” Lynn McGuire <lynnmcguire5@gmail.com> - 2023-10-05 17:40 -0500
    Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-05 15:57 -0700
    Re: “ChattyG takes a college freshman C/C++ programming exam” Christian Gollwitzer <auriocus@gmx.de> - 2023-10-06 07:36 +0200
      Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-06 11:59 -0700
    Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-07 23:54 +0300
      Re: “ChattyG takes a college freshman C/C++ programming exam” jak <nospam@please.ty> - 2023-10-08 09:42 +0200
        Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-08 04:27 -0400
        Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-08 12:55 +0300
          Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-08 13:06 +0300
          Re: “ChattyG takes a college freshman C/C++ programming exam” candycanearter07 <no@thanks.net> - 2023-10-08 06:22 -0500
            Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-08 15:03 +0300
              Re: “ChattyG takes a college freshman C/C++ programming exam” Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-10-15 15:51 -0400
                Re: “ChattyG takes a college freshman C/C++ programming exam” Richard Damon <richard@damon-family.org> - 2023-10-15 17:53 -0400
                Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-16 00:07 -0400
                  Re: “ChattyG takes a college freshman C/C++ programming exam” Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2023-10-16 22:01 -0400
                    Re: “ChattyG takes a college freshman C/C++ programming exam” Richard Damon <richard@damon-family.org> - 2023-10-16 22:41 -0400
                    Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-17 12:22 -0400
            Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-08 12:31 -0700
              Re: “ChattyG takes a college freshman C/C++ programming exam” Richard Damon <news.x.richarddamon@xoxy.net> - 2023-10-08 21:17 -0400
                Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-09 01:13 -0400
                  Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-10 12:18 -0700
              Re: “ChattyG takes a college freshman C/C++ programming exam” MarioCCCP <NoliMihiFrangereMentulam@libero.it> - 2023-10-13 11:22 +0200
                Re: “ChattyG takes a college freshman C/C++ programming exam” Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-10-13 10:04 -0700
          Re: “ChattyG takes a college freshman C/C++ programming exam” Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-10-08 13:54 +0100
          Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-08 11:25 -0400
            Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-08 20:07 +0300
          Re: “ChattyG takes a college freshman C/C++ programming exam” Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2023-10-08 16:37 +0000
            Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-08 20:05 +0300
              Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-09 01:13 -0400
                Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-09 13:30 -0700
                  Re: Re: “ChattyG takes a college freshman C/C++ programming exam” scott@slp53.sl.home (Scott Lurndal) - 2023-10-09 20:33 +0000
                    Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-10 12:04 -0700
              Re: “ChattyG takes a college freshman C/C++ programming exam” Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2023-10-09 12:17 +0000
                Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-09 23:03 +0300
                  Re: “ChattyG takes a college freshman C/C++ programming exam” scott@slp53.sl.home (Scott Lurndal) - 2023-10-09 20:30 +0000
                    Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-09 23:45 +0300
                      Re: “ChattyG takes a college freshman C/C++ programming exam” scott@slp53.sl.home (Scott Lurndal) - 2023-10-09 21:21 +0000
                      Re: “ChattyG takes a college freshman C/C++ programming exam” scott@slp53.sl.home (Scott Lurndal) - 2023-10-09 21:26 +0000
                      Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-09 22:41 -0400
                  Re: “ChattyG takes a college freshman C/C++ programming exam” Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-10-09 21:47 +0100
            Re: “ChattyG takes a college freshman C/C++ programming exam” Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-10-08 15:31 -0700
              Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-09 01:12 -0400
                Re: “ChattyG takes a college freshman C/C++ programming exam” Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-10-10 18:46 -0700
                  Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-11 01:14 -0400
                  Re: “ChattyG takes a college freshman C/C++ programming exam” scott@slp53.sl.home (Scott Lurndal) - 2023-10-11 13:47 +0000
                  Re: “ChattyG takes a college freshman C/C++ programming exam” Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-10-11 07:56 -0700
              Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@gmail.moc> - 2023-10-09 23:06 +0300
                Re: “ChattyG takes a college freshman C/C++ programming exam” Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-10-09 17:15 -0700
        Re: “ChattyG takes a college freshman C/C++ programming exam” James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-10-08 11:08 -0400
    Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-11 21:06 -0700
      Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-11 21:10 -0700
      Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-10-12 10:56 +0300
      Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-10-12 10:57 +0300
        Re: “ChattyG takes a college freshman C/C++ programming exam” "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-10-12 12:59 -0700
    Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-10-12 10:53 +0300
      Re: “ChattyG takes a college freshman C/C++ programming exam” Phil Carmody <pc+usenet@asdf.org> - 2023-10-23 15:39 +0300
        Re: “ChattyG takes a college freshman C/C++ programming exam” Anton Shepelev <anton.txt@g{oogle}mail.com> - 2023-10-23 16:45 +0300

Page 1 of 3  [1] 2 3  Next page →


#92398 — “ChattyG takes a college freshman C/C++ programming exam”

FromLynn McGuire <lynnmcguire5@gmail.com>
Date2023-10-05 17:40 -0500
Subject“ChattyG takes a college freshman C/C++ programming exam”
Message-ID<ufne0r$15ple$1@dont-email.me>
“ChattyG takes a college freshman C/C++ programming exam”
    https://www.theregister.com/2023/10/03/chatgpt_code_college/

“ChatGPT was put to the test via a series of humdrum freshman C/C++ 
programming tasks and it passed – though not with honors.”

“According to a Croatian research team, while first-year students can 
struggle with some of the assignments, the results [PDF] showed ChatGPT 
hitting proficiency targets that ranged between average and that of 
experienced programmers. And naturally, as with all college exams, 
outcomes can be determined by how questions are worded.”

Lynn

[toc] | [next] | [standalone]


#92399

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-10-05 15:57 -0700
Message-ID<ufnf1b$15tsq$1@dont-email.me>
In reply to#92398
On 10/5/2023 3:40 PM, Lynn McGuire wrote:
> “ChattyG takes a college freshman C/C++ programming exam”
>     https://www.theregister.com/2023/10/03/chatgpt_code_college/
> 
> “ChatGPT was put to the test via a series of humdrum freshman C/C++ 
> programming tasks and it passed – though not with honors.”
> 
> “According to a Croatian research team, while first-year students can 
> struggle with some of the assignments, the results [PDF] showed ChatGPT 
> hitting proficiency targets that ranged between average and that of 
> experienced programmers. And naturally, as with all college exams, 
> outcomes can be determined by how questions are worded.”

If we keep teaching it, it should get better and better... :^o

[toc] | [prev] | [next] | [standalone]


#92406

FromChristian Gollwitzer <auriocus@gmx.de>
Date2023-10-06 07:36 +0200
Message-ID<ufo6fb$1ddit$1@dont-email.me>
In reply to#92398
Am 06.10.23 um 00:40 schrieb Lynn McGuire:
> “ChattyG takes a college freshman C/C++ programming exam”
>     https://www.theregister.com/2023/10/03/chatgpt_code_college/
> 
> “ChatGPT was put to the test via a series of humdrum freshman C/C++ 
> programming tasks and it passed – though not with honors.”

This is already outdated. I've read the paper and they used the free 
version, aka GPT-3.5. They should repeat the same tests with at least 
GPT-4. There is a vast improvement especially in programming skill from 
3.5 to 4.

In addition, there are open source models available which you can run on 
your own computer [1], some of them are specifically finetuned towards 
coding - e.g. WizardCoder [2]. The version finetuned to Python 
specifically is claimed to surpass GPT-4 in some benchmarks. It can be 
downloaded in quantized form, ready to run even CPU-only on a machine 
with sufficient RAM [1,3]

[1] https://github.com/ggerganov/llama.cpp
[2] https://github.com/nlpxucan/WizardLM/
[3] https://huggingface.co/TheBloke/WizardCoder-Python-34B-V1.0-GGUF

	Christian

[toc] | [prev] | [next] | [standalone]


#92418

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-10-06 11:59 -0700
Message-ID<ufplfa$1rmlt$1@dont-email.me>
In reply to#92406
On 10/5/2023 10:36 PM, Christian Gollwitzer wrote:
> Am 06.10.23 um 00:40 schrieb Lynn McGuire:
>> “ChattyG takes a college freshman C/C++ programming exam”
>>     https://www.theregister.com/2023/10/03/chatgpt_code_college/
>>
>> “ChatGPT was put to the test via a series of humdrum freshman C/C++ 
>> programming tasks and it passed – though not with honors.”
> 
> This is already outdated. I've read the paper and they used the free 
> version, aka GPT-3.5. They should repeat the same tests with at least 
> GPT-4. There is a vast improvement especially in programming skill from 
> 3.5 to 4.
> 
> In addition, there are open source models available which you can run on 
> your own computer [1], some of them are specifically finetuned towards 
> coding - e.g. WizardCoder [2]. The version finetuned to Python 
> specifically is claimed to surpass GPT-4 in some benchmarks. It can be 
> downloaded in quantized form, ready to run even CPU-only on a machine 
> with sufficient RAM [1,3]
> 
> [1] https://github.com/ggerganov/llama.cpp
> [2] https://github.com/nlpxucan/WizardLM/
> [3] https://huggingface.co/TheBloke/WizardCoder-Python-34B-V1.0-GGUF

Thanks for the links. :^)

[toc] | [prev] | [next] | [standalone]


#92423

FromAnton Shepelev <anton.txt@gmail.moc>
Date2023-10-07 23:54 +0300
Message-ID<20231007235448.25c4986f123c88ff65d2f103@gmail.moc>
In reply to#92398
Lynn McGuire:

> ChattyG takes a college freshman C/C++ programming exam
> https://www.theregister.com/2023/10/03/chatgpt_code_college/

For me, it failed the very trivia:

AS: Can a C function return a pointer to itself?

CG: Yes, a C function can return a pointer to itself. This
    is known as a recursive function, where a function calls
    itself repeatedly until a certain condition is met. In
    this case, the function would return a pointer to the
    function itself to enable the recursive call.

I asked that question here:

                <https://app.chatty-g.com/>
-- 
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments

[toc] | [prev] | [next] | [standalone]


#92428

Fromjak <nospam@please.ty>
Date2023-10-08 09:42 +0200
Message-ID<uftmh9$2vieq$1@dont-email.me>
In reply to#92423
Anton Shepelev ha scritto:
> Lynn McGuire:
> 
>> ChattyG takes a college freshman C/C++ programming exam
>> https://www.theregister.com/2023/10/03/chatgpt_code_college/
> 
> For me, it failed the very trivia:
> 
> AS: Can a C function return a pointer to itself?
> 
> CG: Yes, a C function can return a pointer to itself. This
>      is known as a recursive function, where a function calls
>      itself repeatedly until a certain condition is met. In
>      this case, the function would return a pointer to the
>      function itself to enable the recursive call.
> 
> I asked that question here:
> 
>                  <https://app.chatty-g.com/>
> 

The explanation is certainly wrong but it is true that it is possible:

#include <stdio.h>

typedef void (* fptr)(int) ;

fptr foo(int n)
{
     printf("Call n. %d\n", n);

     return (fptr)foo;
}

int main()
{
     fptr p = foo(1);

     p(2);

     return 0;
}

Out:

Call n. 1
Call n. 2

[toc] | [prev] | [next] | [standalone]


#92429

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2023-10-08 04:27 -0400
Message-ID<uftp5s$2vmsp$1@dont-email.me>
In reply to#92428
On 10/8/23 03:42, jak wrote:
> Anton Shepelev ha scritto:
>> Lynn McGuire:
>>
>>> ChattyG takes a college freshman C/C++ programming exam
>>> https://www.theregister.com/2023/10/03/chatgpt_code_college/
>>
>> For me, it failed the very trivia:
>>
>> AS: Can a C function return a pointer to itself?
>>
>> CG: Yes, a C function can return a pointer to itself. This
>> is known as a recursive function, where a function calls
>> itself repeatedly until a certain condition is met. In
>> this case, the function would return a pointer to the
>> function itself to enable the recursive call.
>>
>> I asked that question here:
>>
>> <https://app.chatty-g.com/>
>>
>
> The explanation is certainly wrong but it is true that it is possible:
>
> #include <stdio.h>
>
> typedef void (* fptr)(int) ;
>
> fptr foo(int n)
> {
> printf("Call n. %d\n", n);
>
> return (fptr)foo;
> }
>
> int main()
> {
> fptr p = foo(1);
>
> p(2);

The return type of *p is void; the return type of foo is fptr.

"Calling a function through an expression whose function type is
different from the function type of the called
function’s definition results in undefined behavior." (7.6.1.2p6)

[toc] | [prev] | [next] | [standalone]


#92430

FromAnton Shepelev <anton.txt@gmail.moc>
Date2023-10-08 12:55 +0300
Message-ID<20231008125543.518cab44cdefed7e8af393c8@gmail.moc>
In reply to#92428
jak to Anton Shepelev:

> > AS: Can a C function return a pointer to itself?
> > CG: Yes, a C function can return a pointer to itself.
>
> The explanation is certainly wrong but it is true that it
> is possible:

I disagree.  A function returns the value of its declared
return type.  And although C allows the definition of self-
referential structs, it does allow the declaration of self-
referencial functions.

> #include <stdio.h>
>
> typedef void (* fptr)(int) ;
>
> fptr foo(int n)
> {
>      printf("Call n. %d\n", n);
>      return (fptr)foo;
> }
>
> int main()
> {
>      fptr p = foo(1);
>      p(2);
>      return 0;
> }
>
> Out:
>
> Call n. 1
> Call n. 2

You are converting void to function pointer fptr.  This is
not supported in C.  You might have taken advantage of
void*, which is silently convertible to any pointer, but
then again it does not work with function pointer, and C has
not analog of void* for functions pointers, so an explicit
conversion is seem inevitable.

-- 
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments

[toc] | [prev] | [next] | [standalone]


#92431

FromAnton Shepelev <anton.txt@gmail.moc>
Date2023-10-08 13:06 +0300
Message-ID<20231008130608.bc41b2bedd1c1c458a434c75@gmail.moc>
In reply to#92430
Anton Shepelev:

> And although C allows the definition of self-referential
> structs, it does allow the declaration of self-referencial
> functions.

does /not/ allow.

-- 
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments

[toc] | [prev] | [next] | [standalone]


#92437

Fromcandycanearter07 <no@thanks.net>
Date2023-10-08 06:22 -0500
Message-ID<ufu3e1$2voq4$2@dont-email.me>
In reply to#92430
On 10/8/23 04:55, Anton Shepelev wrote:
> I disagree.  A function returns the value of its declared
> return type.  And although C allows the definition of self-
> referential structs, it does allow the declaration of self-
> referencial functions.

What if you typedef'd the function pointer?

Also, you could probably get away with it by just setting it to a void 
pointer and casting the return value to the function pointer.
-- 
user <candycane> is generated from /dev/urandom

[toc] | [prev] | [next] | [standalone]


#92438

FromAnton Shepelev <anton.txt@gmail.moc>
Date2023-10-08 15:03 +0300
Message-ID<20231008150352.5dfc8262244889e65a132733@gmail.moc>
In reply to#92437
candycanearter07 to Anton Shepelev:

> > And although C allows the definition of self-referential
> > structs, it does [not] allow the declaration of self-
> > referencial functions.
>
> What if you typedef'd the function pointer?

That's what I did:

   typedef void   (*fp   )( void );
   typedef stm_fp (*func )( struct my_struct * s, int i );

As you see, the `func' still must return something other
than func...

> Also, you could probably get away with it by just setting
> it to a void pointer and casting the return value to the
> function pointer.

Yes, to a void /function/ pointer, because void* is
incompatible with void(*)(void).  And then it is not a
truely recursive declaration, and requires additional work
to invoke.  And the following does not compile, of course:

   typedef func (*func )( struct my_struct * s, int i );

-- 
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments

[toc] | [prev] | [next] | [standalone]


#92865

FromPavel <pauldontspamtolk@removeyourself.dontspam.yahoo>
Date2023-10-15 15:51 -0400
Message-ID<PpXWM.24780$w4ec.17265@fx14.iad>
In reply to#92438
Anton Shepelev wrote:
> candycanearter07 to Anton Shepelev:
> 
>>> And although C allows the definition of self-referential
>>> structs, it does [not] allow the declaration of self-
>>> referencial functions.
>>
>> What if you typedef'd the function pointer?
> 
> That's what I did:
> 
>     typedef void   (*fp   )( void );
>     typedef stm_fp (*func )( struct my_struct * s, int i );
> 
> As you see, the `func' still must return something other
> than func...
> 
>> Also, you could probably get away with it by just setting
>> it to a void pointer and casting the return value to the
>> function pointer.
> 
> Yes, to a void /function/ pointer, because void* is
> incompatible with void(*)(void).  And then it is not a
> truely recursive declaration, and requires additional work
> to invoke.  
...

The below seems to work in gcc, unsure if this is standard-compliant but 
does not require extra work to invoke. If it is, I understand that the 
fptr type is not that of pointer to f, but your question does not 
qualify whether the function should return the value of a pointer to 
itself or a pointer of type of itself or both. If the former, the answer 
seems to be "yes" because the second call by pointer seems to work, no?


typedef void* (*vfptr_t)();

vfptr_t f(int n)
{
   printf("I am f n=%d\n", n);
   return (vfptr_t)&f;
}

int
main(int argc, char* argv[])
{
   vfptr_t fptr = f(5);
   fptr(6);
   return 0;
}

[toc] | [prev] | [next] | [standalone]


#92871

FromRichard Damon <richard@damon-family.org>
Date2023-10-15 17:53 -0400
Message-ID<ughn0b$1kkth$2@i2pn2.org>
In reply to#92865
On 10/15/23 3:51 PM, Pavel wrote:
> Anton Shepelev wrote:
>> candycanearter07 to Anton Shepelev:
>>
>>>> And although C allows the definition of self-referential
>>>> structs, it does [not] allow the declaration of self-
>>>> referencial functions.
>>>
>>> What if you typedef'd the function pointer?
>>
>> That's what I did:
>>
>>     typedef void   (*fp   )( void );
>>     typedef stm_fp (*func )( struct my_struct * s, int i );
>>
>> As you see, the `func' still must return something other
>> than func...
>>
>>> Also, you could probably get away with it by just setting
>>> it to a void pointer and casting the return value to the
>>> function pointer.
>>
>> Yes, to a void /function/ pointer, because void* is
>> incompatible with void(*)(void).  And then it is not a
>> truely recursive declaration, and requires additional work
>> to invoke. 
> ...
> 
> The below seems to work in gcc, unsure if this is standard-compliant but 
> does not require extra work to invoke. If it is, I understand that the 
> fptr type is not that of pointer to f, but your question does not 
> qualify whether the function should return the value of a pointer to 
> itself or a pointer of type of itself or both. If the former, the answer 
> seems to be "yes" because the second call by pointer seems to work, no?
> 
> 
> typedef void* (*vfptr_t)();
> 
> vfptr_t f(int n)
> {
>    printf("I am f n=%d\n", n);
>    return (vfptr_t)&f;
> }
> 
> int
> main(int argc, char* argv[])
> {
>    vfptr_t fptr = f(5);
>    fptr(6);
>    return 0;
> }

No, it doesn't meet the strict definition of the challenge, as if it 
did, then the following would work:


int
main()
{
     vfptr_t fptr1 = f(5);
     vfptr_t fptr2 = fptr1(6);
     return 0;
}

as the pointer needs to act just like the function.

It seems that the best that can be done is to have the function actually 
return a pointer to some function type, and then cast that to another 
type that represents a function that returns a pointer the that function 
type.

The issue is that to make it return the right type without needing a 
cast, forces an infinite level of indirection in the type, while with 
the cast, in adding one level, makes things work.

The problem is that in calling, we are effectively removing a level of 
indirection from the definition and the only sort of number that can be 
reduced by 1 and still be the same is an infinite number.

[toc] | [prev] | [next] | [standalone]


#92875

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2023-10-16 00:07 -0400
Message-ID<ugictr$vur3$1@dont-email.me>
In reply to#92865
On 10/15/23 15:51, Pavel wrote:
> Anton Shepelev wrote:
>> candycanearter07 to Anton Shepelev:
>>
>>>> And although C allows the definition of self-referential
>>>> structs, it does [not] allow the declaration of self-
>>>> referencial functions.

Just for the sake of those who might not remember (I'm not suggesting
that you are one of them), this discussion is about C, even though
cross-posted to comp.lang.c++. Citations below are from the C standard.

...
> The below seems to work in gcc, unsure if this is standard-compliant but 
> does not require extra work to invoke. If it is, I understand that the 
> fptr type is not that of pointer to f, but your question does not 
> qualify whether the function should return the value of a pointer to 
> itself or a pointer of type of itself or both. If the former, the answer 
> seems to be "yes" because the second call by pointer seems to work, no?
> 
> 
> typedef void* (*vfptr_t)();
> 
> vfptr_t f(int n)
> {
>    printf("I am f n=%d\n", n);
>    return (vfptr_t)&f;
> }
> 
> int
> main(int argc, char* argv[])
> {
>    vfptr_t fptr = f(5);
>    fptr(6);
>    return 0;
> }

"If the function is defined with a type that is not compatible with the
type (of the expression) pointed to by the expression that denotes the
called function, the behavior is undefined." (6.5.2.2p9)

The type of f() is "vfptr_t f(int)". The type pointed at by fptr is
"void*()".

"For two function types to be compatible, both shall specify compatible
return types." (6.7.5.3p15)

void* and vfptr_t are not compatible types, so the type of f() is not
compatible with the type pointed at by fptr. The behavior is undefined,
even though your code makes no use of the value returned by that function.

[toc] | [prev] | [next] | [standalone]


#92892

FromPavel <pauldontspamtolk@removeyourself.dontspam.yahoo>
Date2023-10-16 22:01 -0400
Message-ID<aWlXM.71246$tnmf.36958@fx09.iad>
In reply to#92875
James Kuyper wrote:
> On 10/15/23 15:51, Pavel wrote:
>> Anton Shepelev wrote:
>>> candycanearter07 to Anton Shepelev:
>>>
>>>>> And although C allows the definition of self-referential
>>>>> structs, it does [not] allow the declaration of self-
>>>>> referencial functions.
> 
> Just for the sake of those who might not remember (I'm not suggesting
> that you are one of them), this discussion is about C, even though
> cross-posted to comp.lang.c++. Citations below are from the C standard.
> 
> ...
>> The below seems to work in gcc, unsure if this is standard-compliant but
>> does not require extra work to invoke. If it is, I understand that the
>> fptr type is not that of pointer to f, but your question does not
>> qualify whether the function should return the value of a pointer to
>> itself or a pointer of type of itself or both. If the former, the answer
>> seems to be "yes" because the second call by pointer seems to work, no?
>>
>>
>> typedef void* (*vfptr_t)();
>>
>> vfptr_t f(int n)
>> {
>>     printf("I am f n=%d\n", n);
>>     return (vfptr_t)&f;
>> }
>>
>> int
>> main(int argc, char* argv[])
>> {
>>     vfptr_t fptr = f(5);
>>     fptr(6);
>>     return 0;
>> }
> 
> "If the function is defined with a type that is not compatible with the
> type (of the expression) pointed to by the expression that denotes the
> called function, the behavior is undefined." (6.5.2.2p9)
> 
> The type of f() is "vfptr_t f(int)". The type pointed at by fptr is
> "void*()".
yes, I noticed. this works as well (with same parameter types):

/* cut here -----------------*/
#include <stdio.h>

typedef void* (*vfptr_t)(int);

vfptr_t f(int n)
{
   printf("I am f n=%d\n", n);
   return (vfptr_t)f;
}

int
main(int argc, char* argv[])
{
   (void)argc;
   (void)argv;
   vfptr_t fptr = f(5);
   fptr(6);
   return 0;
}
/* cut here -----------------*/


> 
> "For two function types to be compatible, both shall specify compatible
> return types." (6.7.5.3p15)
I see, thanks.
> 
> void* and vfptr_t are not compatible types, so the type of f() is not
> compatible with the type pointed at by fptr. The behavior is undefined,
> even though your code makes no use of the value returned by that function.
Doesn't it use the returned value by assigning it to fptr and then 
calling f thru it?

[toc] | [prev] | [next] | [standalone]


#92893

FromRichard Damon <richard@damon-family.org>
Date2023-10-16 22:41 -0400
Message-ID<ugks9k$1pa8g$5@i2pn2.org>
In reply to#92892
On 10/16/23 10:01 PM, Pavel wrote:
> James Kuyper wrote:
>> On 10/15/23 15:51, Pavel wrote:
>>> Anton Shepelev wrote:
>>>> candycanearter07 to Anton Shepelev:
>>>>
>>>>>> And although C allows the definition of self-referential
>>>>>> structs, it does [not] allow the declaration of self-
>>>>>> referencial functions.
>>
>> Just for the sake of those who might not remember (I'm not suggesting
>> that you are one of them), this discussion is about C, even though
>> cross-posted to comp.lang.c++. Citations below are from the C standard.
>>
>> ...
>>> The below seems to work in gcc, unsure if this is standard-compliant but
>>> does not require extra work to invoke. If it is, I understand that the
>>> fptr type is not that of pointer to f, but your question does not
>>> qualify whether the function should return the value of a pointer to
>>> itself or a pointer of type of itself or both. If the former, the answer
>>> seems to be "yes" because the second call by pointer seems to work, no?
>>>
>>>
>>> typedef void* (*vfptr_t)();
>>>
>>> vfptr_t f(int n)
>>> {
>>>     printf("I am f n=%d\n", n);
>>>     return (vfptr_t)&f;
>>> }
>>>
>>> int
>>> main(int argc, char* argv[])
>>> {
>>>     vfptr_t fptr = f(5);
>>>     fptr(6);
>>>     return 0;
>>> }
>>
>> "If the function is defined with a type that is not compatible with the
>> type (of the expression) pointed to by the expression that denotes the
>> called function, the behavior is undefined." (6.5.2.2p9)
>>
>> The type of f() is "vfptr_t f(int)". The type pointed at by fptr is
>> "void*()".
> yes, I noticed. this works as well (with same parameter types):
> 
> /* cut here -----------------*/
> #include <stdio.h>
> 
> typedef void* (*vfptr_t)(int);
> 
> vfptr_t f(int n)
> {
>    printf("I am f n=%d\n", n);
>    return (vfptr_t)f;
> }
> 
> int
> main(int argc, char* argv[])
> {
>    (void)argc;
>    (void)argv;
>    vfptr_t fptr = f(5);
>    fptr(6);
>    return 0;
> }
> /* cut here -----------------*/
> 
> 
>>
>> "For two function types to be compatible, both shall specify compatible
>> return types." (6.7.5.3p15)
> I see, thanks.
>>
>> void* and vfptr_t are not compatible types, so the type of f() is not
>> compatible with the type pointed at by fptr. The behavior is undefined,
>> even though your code makes no use of the value returned by that 
>> function.
> Doesn't it use the returned value by assigning it to fptr and then 
> calling f thru it?
> 
> 

The issue is that the call to fptr(6) thinks it is calling a function 
that return a void*, but it is actually calling a function that returns 
a pointer to a function that returns a void*, those aren't the same type 
of function, so you get undefined behavior.

And that is why you needed the cast in the function.

It is also why if you made the second call into:

     vfptr_t fptr2 = fptr(6);

you would get an error, as the function type of fptr doesn't return 
something compatible with vfptr_t. That is using the return value of the 
call through the returned function pointer, as that is where the error is.

[toc] | [prev] | [next] | [standalone]


#92996

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2023-10-17 12:22 -0400
Message-ID<ugmccu$328ca$1@dont-email.me>
In reply to#92892
On 10/17/23 07:05, Anton Shepelev wrote:
> James Kuyper:
>
>> One of the most common and most dangerous things that can
>> happen when the behavior of code is undefined, is that it
>> happens to behave in precisely the fashion you
>> unjustifiably expected it to behave, leaving you unaware
>> of the [Ant: a?] flaw in your program.

You're right - "a" works better; we don't know that it's the only flaw.

[toc] | [prev] | [next] | [standalone]


#92450

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2023-10-08 12:31 -0700
Message-ID<ufv02a$398hi$1@dont-email.me>
In reply to#92437
On 10/8/2023 4:22 AM, candycanearter07 wrote:
> On 10/8/23 04:55, Anton Shepelev wrote:
>> I disagree.  A function returns the value of its declared
>> return type.  And although C allows the definition of self-
>> referential structs, it does allow the declaration of self-
>> referencial functions.
> 
> What if you typedef'd the function pointer?
> 
> Also, you could probably get away with it by just setting it to a void 
> pointer and casting the return value to the function pointer.

Not sure if that's allowed. A function pointer cast to a void pointer, 
which in turn is cast back to a function pointer is undefined? What am I 
forgetting here?

[toc] | [prev] | [next] | [standalone]


#92454

FromRichard Damon <news.x.richarddamon@xoxy.net>
Date2023-10-08 21:17 -0400
Message-ID<ufvkar$3e7t0$1@dont-email.me>
In reply to#92450
On 10/8/23 3:31 PM, Chris M. Thomasson wrote:
> On 10/8/2023 4:22 AM, candycanearter07 wrote:
>> On 10/8/23 04:55, Anton Shepelev wrote:
>>> I disagree.  A function returns the value of its declared
>>> return type.  And although C allows the definition of self-
>>> referential structs, it does allow the declaration of self-
>>> referencial functions.
>>
>> What if you typedef'd the function pointer?
>>
>> Also, you could probably get away with it by just setting it to a void 
>> pointer and casting the return value to the function pointer.
> 
> Not sure if that's allowed. A function pointer cast to a void pointer, 
> which in turn is cast back to a function pointer is undefined? What am I 
> forgetting here?

While not specifically called out as undefined behavior, it also isn't 
ever said to be allowed.

It will work on the most common processors currently, but I have an 
active product using a processor where it will definitely NOT work, as 
function pointers are bigger than object pointers.

The Posix standard sort of requires that it works, but only for a 
certain set of function pointers, so you can do a work around for 
machines that don't actually support it.

One way to acheive the results is define the return type as a pointer to 
some defined type of function, and you cast the return value to a 
pointer to a function that returns that type of function, and in the 
function cast the pointer to itself to that type of function.

i.e.


typedef void (*fret)();	// fret is a simplifed type we can return
typedef fret (*pf)();   // pf is the actual type of the function.

fret func() {
    return (fret) func;
}

int main() {

     pf p = (pf)func(); // Call the function and cast the return value
     pf p2 = (pf)p();   // call the same function from the pointer
}

Note, the standard specifically says we can cast any function pointer 
type to another function pointer type and back and get a usable pointer.

(untested code, but should be at least close)

[toc] | [prev] | [next] | [standalone]


#92459

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2023-10-09 01:13 -0400
Message-ID<ug0251$3lhhc$2@dont-email.me>
In reply to#92454
On 10/8/23 21:17, Richard Damon wrote:
> On 10/8/23 3:31 PM, Chris M. Thomasson wrote:
...
>> Not sure if that's allowed. A function pointer cast to a void
>> pointer, which in turn is cast back to a function pointer is
>> undefined? What am I forgetting here?
>
> While not specifically called out as undefined behavior, it also isn't
> ever said to be allowed.

Precisely - it is undefined "... by the omission of any explicit
definition of behavior." (4p2

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c++


csiph-web