questionable cast

R

Rouben Rostamian

I ran into a few questionable C programming practices while reading
a very nice tutorial on pthreads in:

http://www.llnl.gov/computing/tutorials/workshops/workshop/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.
 
B

Ben Pfaff

I ran into a few questionable C programming practices while reading
a very nice tutorial on pthreads in:

http://www.llnl.gov/computing/tutorials/workshops/workshop/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;
}

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;
}
 
R

Rouben Rostamian

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.
 
P

pete

Rouben said:
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 ?
 
R

Rouben Rostamian

Rouben said:
(e-mail address removed) (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.
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.
 
B

Barry Schwarz

I ran into a few questionable C programming practices while reading
a very nice tutorial on pthreads in:

http://www.llnl.gov/computing/tutorials/workshops/workshop/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>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top