Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > questionable cast

Reply
Thread Tools

questionable cast

 
 
Rouben Rostamian
Guest
Posts: n/a
 
      01-18-2004
I ran into a few questionable C programming practices while reading
a very nice tutorial on pthreads in:

http://www.llnl.gov/computing/tutori...kshop/pthreads

For instance, in several places we have code similar to the following:

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

#include <stdio.h>

void foo(void *arg)
{
printf("the number is %d\n", arg);
}

int main(void)
{
int i;

for (i=0; i<5; i++)
foo((void *)i);

return 0;
}

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

Is there any way that one can justify the cast from int to (void *)
and back? Probably the printf() statement can be made a bit more
digestible with an additional cast, as in:

printf("the number is %d\n", (int)arg);

but even then, the whole effort looks suspicious.

Elsewhere in the same web page we have:

int status;
...
bar((void **)&status);

which in effect is a variation on the previous theme.


--
Rouben Rostamian
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-18-2004
(Rouben Rostamian) writes:

> I ran into a few questionable C programming practices while reading
> a very nice tutorial on pthreads in:
>
> http://www.llnl.gov/computing/tutori...kshop/pthreads
>
> For instance, in several places we have code similar to the following:
>
> ------------------------------
>
> #include <stdio.h>
>
> void foo(void *arg)
> {
> printf("the number is %d\n", arg);
> }
>
> int main(void)
> {
> int i;
>
> for (i=0; i<5; i++)
> foo((void *)i);
>
> return 0;
> }
>
> ------------------------------
>
> Is there any way that one can justify the cast from int to (void *)
> and back? [...]


No, especially given this, much cleaner, alternative:

void foo(void *arg_)
{
int *arg = arg_;
printf("the number is %d\n", *arg);
}

int main(void)
{
int i;

for (i=0; i<5; i++)
foo(&i);

return 0;
}
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
 
 
 
Rouben Rostamian
Guest
Posts: n/a
 
      01-19-2004
In article <>,
Ben Pfaff <> wrote:
> (Rouben Rostamian) writes:
>
>> I ran into a few questionable C programming practices while reading
>> a very nice tutorial on pthreads in:
>>
>> http://www.llnl.gov/computing/tutori...kshop/pthreads
>>
>> For instance, in several places we have code similar to the following:
>>
>> ------------------------------
>>
>> #include <stdio.h>
>>
>> void foo(void *arg)
>> {
>> printf("the number is %d\n", arg);
>> }
>>
>> int main(void)
>> {
>> int i;
>>
>> for (i=0; i<5; i++)
>> foo((void *)i);
>>
>> return 0;
>> }
>>
>> ------------------------------
>>
>> Is there any way that one can justify the cast from int to (void *)
>> and back? [...]

>
>No, especially given this, much cleaner, alternative:
>
>void foo(void *arg_)
>{
> int *arg = arg_;
> printf("the number is %d\n", *arg);
>}
>
>int main(void)
>{
> int i;
>
> for (i=0; i<5; i++)
> foo(&i);
>
> return 0;
>}


Thanks, Dan, for clarifying this.

In fact, the alternate solution that you have offered is the obvious
thing to do in normal circumstances. Unfortunately that solution
is not applicable in the context of the pthreads program that I
was looking at in the tutorial referred to above. That's why the
tutorial resorts to that unorthodox casting to get the program going.
There are other (and legal) ways to get around the dilemma but they
are not relevant to this discussion. My question was about the
legitimacy of casting of int to (void *) which you have answered.

--
Rouben Rostamian
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-19-2004
Rouben Rostamian wrote:
>
> In article <>,
> Ben Pfaff <> wrote:
> > (Rouben Rostamian) writes:
> >
> >> I ran into a few questionable C programming practices while reading
> >> a very nice tutorial on pthreads in:
> >>
> >> http://www.llnl.gov/computing/tutori...kshop/pthreads
> >>
> >> For instance, in several places we have code similar to the following:
> >>
> >> ------------------------------
> >>
> >> #include <stdio.h>
> >>
> >> void foo(void *arg)
> >> {
> >> printf("the number is %d\n", arg);
> >> }
> >>
> >> int main(void)
> >> {
> >> int i;
> >>
> >> for (i=0; i<5; i++)
> >> foo((void *)i);
> >>
> >> return 0;
> >> }
> >>
> >> ------------------------------
> >>
> >> Is there any way that one can justify the cast from int to (void *)
> >> and back? [...]

> >
> >No, especially given this, much cleaner, alternative:
> >
> >void foo(void *arg_)
> >{
> > int *arg = arg_;
> > printf("the number is %d\n", *arg);
> >}
> >
> >int main(void)
> >{
> > int i;
> >
> > for (i=0; i<5; i++)
> > foo(&i);
> >
> > return 0;
> >}

>
> Thanks, Dan, for clarifying this.
>
> In fact, the alternate solution that you have offered is the obvious
> thing to do in normal circumstances.


I don't see the code as being symanticly equivalent.
((void*)i) may varry with the value of i.
&i is constant regardless of the value of i.

Dan who ?

--
pete
 
Reply With Quote
 
Rouben Rostamian
Guest
Posts: n/a
 
      01-19-2004
In the preceding article I wrote:

>Thanks, Dan, for clarifying this.


That should have read: "Thanks, Ben, for clarifying this." with Ben
as in Ben Pfaff <>.

Thanks to pete <> for pointing this out.

I hope that I haven't made any further misattributions in this post.

--
Rouben Rostamian
 
Reply With Quote
 
Rouben Rostamian
Guest
Posts: n/a
 
      01-19-2004
In article <>,
pete <> wrote:
>Rouben Rostamian wrote:
>>
>> In article <>,
>> Ben Pfaff <> wrote:
>> > (Rouben Rostamian) writes:
>> >
>> >> ------------------------------
>> >>
>> >> #include <stdio.h>
>> >>
>> >> void foo(void *arg)
>> >> {
>> >> printf("the number is %d\n", arg);
>> >> }
>> >>
>> >> int main(void)
>> >> {
>> >> int i;
>> >>
>> >> for (i=0; i<5; i++)
>> >> foo((void *)i);
>> >>
>> >> return 0;
>> >> }
>> >>
>> >> ------------------------------
>> >>
>> >> Is there any way that one can justify the cast from int to (void *)
>> >> and back? [...]
>> >
>> >No, especially given this, much cleaner, alternative:
>> >
>> >void foo(void *arg_)
>> >{
>> > int *arg = arg_;
>> > printf("the number is %d\n", *arg);
>> >}
>> >
>> >int main(void)
>> >{
>> > int i;
>> >
>> > for (i=0; i<5; i++)
>> > foo(&i);
>> >
>> > return 0;
>> >}

>>
>> Thanks, Dan, for clarifying this.


That should be been "Thanks, Ben". Thanks to Pete for catching this.

>> In fact, the alternate solution that you have offered is the obvious
>> thing to do in normal circumstances.

>
>I don't see the code as being symanticly equivalent.
>((void*)i) may varry with the value of i.
>&i is constant regardless of the value of i.


You are right. The two codes are not semantically equivalent
precisely for the reason that you have pointed out. And it is
for that reason that one of them works with pthreads while the
other doesn't. Unfortunately, the one that works, is not legal C,
as Ben has confirmed. Pthreads programmers have standard (and legal)
tricks to get around this, but that is not relevant to the subject of
this newsgroup so I will drop it here.

--
Rouben Rostamian
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      01-19-2004
On Sun, 18 Jan 2004 23:41:46 +0000 (UTC),
(Rouben Rostamian) wrote:

>I ran into a few questionable C programming practices while reading
>a very nice tutorial on pthreads in:
>
> http://www.llnl.gov/computing/tutori...kshop/pthreads
>
>For instance, in several places we have code similar to the following:
>
>------------------------------
>
>#include <stdio.h>
>
>void foo(void *arg)
>{
> printf("the number is %d\n", arg);


Given the parameter type, the only to legal options are
printf("the number is %p\n", arg);
or
printf("the number is %d\n", (int)arg);
as you noted.

>}
>
>int main(void)
>{
> int i;
>
> for (i=0; i<5; i++)
> foo((void *)i);
>
> return 0;
>}
>
>------------------------------
>
>Is there any way that one can justify the cast from int to (void *)
>and back? Probably the printf() statement can be made a bit more
>digestible with an additional cast, as in:
>
> printf("the number is %d\n", (int)arg);
>
>but even then, the whole effort looks suspicious.
>
>Elsewhere in the same web page we have:
>
> int status;
> ...
> bar((void **)&status);
>
>which in effect is a variation on the previous theme.




<<Remove the del for email>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple inheritance \ questionable design Andrew C++ 2 10-19-2007 11:17 AM
Questionable file Seagull NZ Computing 12 08-11-2005 10:50 AM
Re: Civil Law (Sale of Residential Property) Act 2003... a questionable piece of legislation Nemesis Computer Support 0 07-15-2004 08:49 AM
Questionable compiler warning Thomas Heinz C++ 12 07-09-2004 01:56 AM
questionable file Lu Tze Computer Security 1 07-20-2003 07:12 AM



Advertisments