news article for your approval 
Author Message
 news article for your approval

Hello,

Two questions:

(1) I know that a pointer to a void * isn't portable. What if I did this:

typedef struct { void *p; } PVOID;
typedef PVOID *PPVOID;

Is that non-portable as well because a pointer to a structure is a
pointer to it's first element.

What I am trying to do is the old doubly-linked list trick that the Amiga
does (where the head and tail nodes share the overlapping 0). The problem
is that to portably do this I need a pointer to void pointer, which is
non portable. Any suggesionts?

(2) In my (outdated) copy of the standard, it says that you can assign a
function pointer type to another function pointer type (i.e. generic
function pointers). A friend said this wasn't the case. Is casting
function pointers portable?

Thanks,
Mark G.



Wed, 10 Jun 1998 03:00:00 GMT  
 news article for your approval

]
] (1) I know that a pointer to a void * isn't portable. What if I did this:
]
] typedef struct { void *p; } PVOID;
] typedef PVOID *PPVOID;
]
] Is that non-portable as well because a pointer to a structure is a
] pointer to it's first element.

    You _can_ use a (void **), portably.  Just be sure of one thing:
that the variable really _is_ a (void **).  That is, calling,

        void foo(void **vpp) { static int x = 23; *vpp = (void *)&x; }

as

        void bar(void) { void *vp; foo(&vp); printf("%d\n", *(int *)vp); }

is perfectly portable.  Calling foo() like

        void bar(void) { int *ip; foo((void **)&ip); printf("%d\n", *ip); }

is _not_ portable, because the value that got stored in ip is never
properly cast to (int *)--the compiler didn't properly "know" that the
variable was an (int *) at all.

    So, you can cast to and from (void *); you _cannot_ cast to and from
(void **).  You also cannot cast to and from (PVOID *)--it's the same
thing as (void **).

    The trick you use may be useful, in that the compiler may complain
more loudly if you abuse (PVOID *) than if you abuse (void **).  But, I
wouldn't count on it.

] What I am trying to do is the old doubly-linked list trick that the Amiga
] does (where the head and tail nodes share the overlapping 0). The problem
] is that to portably do this I need a pointer to void pointer, which is
] non portable. Any suggesionts?

    I'm not sure what the Amiga does--someone else probably knows.  But
it sounds like you're type punning (void *) and some other pointer type.
That's no more portable than casting (void **) about, for the same
reasons: because the compiler doesn't know you're doing it, and can't
properly cast the pointers.

] (2) In my (outdated) copy of the standard, it says that you can assign a
] function pointer type to another function pointer type (i.e. generic
] function pointers). A friend said this wasn't the case. Is casting
] function pointers portable?

    Hmmmm,... I don't know of any compiler where you cannot cast one
function pointer type to another.  But it's clear that you can't
actually _use_ such a pointer unless you first cast it back to its
correct (original) type.

                                           -- Bill K.

Bill Kaufman,          | "Patience is a virtue.  Seersucker is a fabric."



Thu, 11 Jun 1998 03:00:00 GMT  
 news article for your approval


 > Hello,
 >
 > Two questions:
 >
 > (1) I know that a pointer to a void * isn't portable. What if I did this:

    void **pptr;

is perfectly valid C and will work on any ANSI compiler. However there is
nothing special about it i.e. you can't think of it as a generic pointer
to pointer. So:

    void **pptr;
    void *ptr;

    pptr = &ptr;

is fine in C whereas:

    void **pptr;
    int *iptr;

    pptr = &iptr;

is not.

 > What I am trying to do is the old doubly-linked list trick that the Amiga
 > does (where the head and tail nodes share the overlapping 0). The problem
 > is that to portably do this I need a pointer to void pointer, which is
 > non portable. Any suggesionts?

If you just want a pointer to a void * object/structure member then simply
use a void **. This is perfectly portable.

 > (2) In my (outdated) copy of the standard, it says that you can assign a
 > function pointer type to another function pointer type (i.e. generic
 > function pointers). A friend said this wasn't the case. Is casting
 > function pointers portable?

Yes, any function pointer may be converted to any other function pointer
type. You must of course convert it back to the original type before
you dereference it (i.e. call the function).

--
-----------------------------------------


-----------------------------------------



Thu, 11 Jun 1998 03:00:00 GMT  
 news article for your approval
--text follows this line--

Quote:
(William Kaufman) writes:

<snip>
   ] typedef struct { void *p; } PVOID;
   ] typedef PVOID *PPVOID;
<snip>
       So, you can cast to and from (void *); you _cannot_ cast to and from
   (void **).  You also cannot cast to and from (PVOID *)--it's the same
   thing as (void **).

Lest it should give the wrong impression, it needs be clarified that
PVOID * is _not_ the same thing as void**: they are similar in the
respect that neither is a 'generic' data pointers and pointers to
aribitrary data cannot be portably converted to/from them. In addition
because void* is the first element of PVOID, PVOID* can be portably
converted to/from void** with explicit casts.

<snip>
       Hmmmm,... I don't know of any compiler where you cannot cast one
   function pointer type to another.  But it's clear that you can't

It is in fact, _required_ by the standard: every function pointer can
be cast to any function pointer type and back with no change.

   actually _use_ such a pointer unless you first cast it back to its
   correct (original) type.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sat, 20 Jun 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. bounced news article - trying again

2. Borland C++ 4.02 (Good News and Bad News)

3. Reply news is listed as separate message in my news reader

4. Microsoft Knowledge Base Article - 122675 Microsoft Knowledge Base Article - 122675

5. C# approval

6. Need program to print bits in data types in CUnread news in comp.lang.c 3982 articles +11329 old

7. Where can I found e-mail news conference about C++ latest news and discusions about C++ like a language ...

8. Have you news about .NET and CORBA ?

9. C++ News Operator

10. Moderation news...

 

 
Powered by phpBB® Forum Software