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


Groups > comp.lang.c > #25896

Re: Do not cast pointers to functions to pointers to primitive types

From "Scott Fluhrer" <sfluhrer@ix.netcom.com>
Newsgroups comp.lang.c
References <fce4988c-58d7-4847-84fe-edb1ed3b722c@googlegroups.com> <20120903180738.297@kylheku.com>
Subject Re: Do not cast pointers to functions to pointers to primitive types
Message-ID <1346774432.563437@rcdn-nntpcache-3> (permalink)
Organization cisco.com
Date 2012-09-04 11:59 -0400

Show all headers | View raw


"Kaz Kylheku" <kaz@kylheku.com> wrote in message 
news:20120903180738.297@kylheku.com...
> On 2012-09-03, ajaybgr <ajay.bgr@gmail.com> wrote:
>> Not sure if this is the right place or OT.
>> One of the Parasoft coding guidelines (which I am using for current 
>> project) is
>> "Do not cast pointers to functions to pointers to primitive types 
>> [CODSTA-09-3]"
>
> [ snip ]
>
>> I can see why it is a bad idea not to write code this way.
>> But can any one explain where this kind of code is useful?
>
> Such code is found in the implementation of shared libraries.
>
> For instance, the dlsym function on POSIX returns void *,
> and this function is used whether you're looking up a function or some 
> other
> kind of symbol.
>
> There is nothing wrong with doing this, except that it's not "maximally
> portable".
>
> For instance, here is one potential problem: what if function pointers are 
> 48
> bits wide and object pointers 32 bits wide.
>
> If you're sure that portability isn't an issue, then it may be the best 
> way to
> solve the kind of problem that it solves, because doing it portably (e.g. 
> with
> unions to store two kinds of pointers in the same space) may add some 
> clutter
> to the program.
>
>> (imo, to be a guideline it must have been used/misused a lot)
>> Where casting a function pointer to primitive type makes sense?
>
> Where you're going to cast it back to a function pointer type later.
>
> Say you have a linked list which stores "void *" items and you want to
> put function pointers in it.

Actually, if you are in such a circumstance (where you need to store a 
function pointer, and some infrastructure only gives you a void * to store 
it in), there is a totally portable way to handle it.  Instead of attempting 
to store the function pointer into the void * type, store a pointer to a 
pointer to the function instead,  For example:

typedef void (*function)(int, double, char *);

void my_function(int a, double b, char *c);
function *p_my_function = &my_function;   /* p_my_function is a pointer to a 
'primitive type', specifically, a function pointer type */


struct linked_list {
    void *data;
}

/* Functions which uses the linked_list type */
struct linked_list *p;
p->data = p_my_function;   /* Completely portable, because we can assign a 
data pointer to a void * type */

...

function *func = p->data;  /* Again, completely portable; the pointer func 
is a data pointer */
(*func)( 7, 3.14159, "Hi ma!" );


-- 
poncho

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


Thread

Do not cast pointers to functions to pointers to primitive types ajaybgr <ajay.bgr@gmail.com> - 2012-09-03 01:50 -0700
  Re: Do not cast pointers to functions to pointers to primitive types Ian Collins <ian-news@hotmail.com> - 2012-09-03 20:59 +1200
  Re: Do not cast pointers to functions to pointers to primitive types Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-09-03 02:13 -0700
  Re: Do not cast pointers to functions to pointers to primitive types Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-09-03 11:24 +0100
  Re: Do not cast pointers to functions to pointers to primitive types James Kuyper <jameskuyper@verizon.net> - 2012-09-03 08:31 -0400
    Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 13:21 -0700
      Re: Do not cast pointers to functions to pointers to primitive types James Kuyper <jameskuyper@verizon.net> - 2012-09-03 16:57 -0400
        Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 14:48 -0700
          Re: Do not cast pointers to functions to pointers to primitive types pacman@kosh.dhis.org (Alan Curry) - 2012-09-03 21:57 +0000
            Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 18:11 -0700
              Re: Do not cast pointers to functions to pointers to primitive types pacman@kosh.dhis.org (Alan Curry) - 2012-09-04 02:03 +0000
                Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 19:20 -0700
                Re: Do not cast pointers to functions to pointers to primitive types pacman@kosh.dhis.org (Alan Curry) - 2012-09-04 20:27 +0000
                Re: Do not cast pointers to functions to pointers to primitive types James Kuyper <jameskuyper@verizon.net> - 2012-09-04 17:10 -0400
                Re: Do not cast pointers to functions to pointers to primitive types Barry Schwarz <schwarzb@dqel.com> - 2012-09-04 14:14 -0700
          Re: Do not cast pointers to functions to pointers to primitive types pete <pfiland@mindspring.com> - 2012-09-03 22:51 -0400
      Re: Do not cast pointers to functions to pointers to primitive types Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2012-09-04 00:02 +0200
        Re: Do not cast pointers to functions to pointers to primitive types Keith Thompson <kst-u@mib.org> - 2012-09-03 18:19 -0700
  Re: Do not cast pointers to functions to pointers to primitive types Kaz Kylheku <kaz@kylheku.com> - 2012-09-04 01:18 +0000
    Re: Do not cast pointers to functions to pointers to primitive types "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2012-09-04 11:59 -0400
      Re: Do not cast pointers to functions to pointers to primitive types Philip Lantz <prl@canterey.us> - 2012-09-06 21:34 -0700

csiph-web