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


Groups > comp.lang.c > #108073

Re: correct way to create dynamically function pointers

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: correct way to create dynamically function pointers
Date 2017-04-12 08:27 +0100
Organization Fix this later
Message-ID <ockknv$ggf$1@dont-email.me> (permalink)
References <b6cbce81-b326-4c26-9079-23e083b2de56@googlegroups.com> <vs8HA.4$eU.3@fx14.iad> <7b6bf43d-6f8a-49b3-4654-ab0f56e7f0e0@verizon.net>

Show all headers | View raw


On 11/04/17 19:11, James R. Kuyper wrote:
> On 04/11/2017 01:34 PM, Scott Lurndal wrote:
>> Hello World <acidlategamer@gmail.com> writes:
> ...
>>> warning: ISO C forbids assignment between function pointer and
>>> =E2=80=98voi=
>>> d *=E2=80=99 [-Wpedantic]
>>>  p =3D malloc(sizeof(p)*4);
>>>    ^
>>>
>>
>> ISO C forbids that because on some architectures, a function pointer
>> may require more bits than a void pointer contains.[*]
>
> It's a more fundamental problem than that: functions and objects are two
> completely different types of things, and pointers to functions are
> therefore fundamentally incompatible with pointers to objects.
>
>>> What am I missing?
>>
>> a cast.  (puts on flameproof suit).
>
> You'll need that suit when the nasal demons come to hunt you down. Where
> would you find the definition of the behavior of such a cast?
> 6.3.2.3 contains no such definition, which is the place where I'd expect
> it to be if the behavior were defined.
>
> Furthermore, since p is a pointer to a function, once you've allocated
> such memory, you'd have to fill it in with a function. Not a pointer to
> a function, as in the OP's code, but an actual function. How would you
> go about doing that? Hint: attempting to memcpy() a function into that
> memory would fail for the same reason that the assignment above failed.

You and Scott have, between you, managed to make this question terribly 
complicated. It was a simple question with a simple, positive answer. 
(In fact, it got the same simple, positive answer more than once, 
because I didn't spot Ben's reply before posting my own.

I suppose, however, that if the simple, positive answer ("yes, it CAN be 
done, and yes, you're NEARLY there, and you just need to tweak THIS 
bit") has been given, we might as well plough a few more furrows in ISO 
9899.

There is one technical point I'd like to make in support of your claim 
that functions and objects are two completely different kinds of things, 
and my point is simply to draw our attention to the existence of this 
well-known software engineering problem:

Objects do, but functions don't.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

correct way to create dynamically function pointers Hello World <acidlategamer@gmail.com> - 2017-04-11 10:20 -0700
  Re: correct way to create dynamically function pointers scott@slp53.sl.home (Scott Lurndal) - 2017-04-11 17:34 +0000
    Re: correct way to create dynamically function pointers Hello World <acidlategamer@gmail.com> - 2017-04-11 10:42 -0700
      Re: correct way to create dynamically function pointers scott@slp53.sl.home (Scott Lurndal) - 2017-04-11 17:48 +0000
        Re: correct way to create dynamically function pointers Hello World <acidlategamer@gmail.com> - 2017-04-11 10:55 -0700
      Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-11 19:06 +0100
      Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-11 14:23 -0400
    Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-11 19:05 +0100
    Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-11 14:11 -0400
      Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 08:27 +0100
        Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-12 13:31 -0400
          Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 23:05 +0100
  Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-11 19:03 +0100
  Re: correct way to create dynamically function pointers "James R. Kuyper" <jameskuyper@verizon.net> - 2017-04-11 14:05 -0400
    Re: correct way to create dynamically function pointers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-04-15 10:19 +0000
      Re: correct way to create dynamically function pointers asetofsymbols@gmail.com - 2017-04-15 05:09 -0700
  Re: correct way to create dynamically function pointers Barry Schwarz <schwarzb@dqel.com> - 2017-04-11 15:13 -0700
    Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 08:28 +0100
  Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 08:19 +0100
    Re: correct way to create dynamically function pointers Spiros Bousbouras <spibou@gmail.com> - 2017-04-12 21:17 +0000
      Re: correct way to create dynamically function pointers Richard Heathfield <rjh@cpax.org.uk> - 2017-04-12 23:11 +0100
        Re: correct way to create dynamically function pointers Keith Thompson <kst-u@mib.org> - 2017-04-12 16:36 -0700
  Re: correct way to create dynamically function pointers John Bode <jfbode1029@gmail.com> - 2017-04-12 10:04 -0700
    Re: correct way to create dynamically function pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-12 19:28 +0100
    Re: correct way to create dynamically function pointers Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-17 05:50 -0700

csiph-web