questionable cast

Discussion in 'C Programming' started by Rouben Rostamian, Jan 18, 2004.

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


    --
    Rouben Rostamian
    Rouben Rostamian, Jan 18, 2004
    #1
    1. Advertising

  2. Rouben Rostamian

    Ben Pfaff Guest

    (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/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? [...]


    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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, Jan 18, 2004
    #2
    1. Advertising

  3. 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/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? [...]

    >
    >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
    Rouben Rostamian, Jan 19, 2004
    #3
  4. Rouben Rostamian

    pete Guest

    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/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? [...]

    > >
    > >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
    pete, Jan 19, 2004
    #4
  5. 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
    Rouben Rostamian, Jan 19, 2004
    #5
  6. 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
    Rouben Rostamian, Jan 19, 2004
    #6
  7. 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/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>>
    Barry Schwarz, Jan 19, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Heinz

    Questionable compiler warning

    Thomas Heinz, Jul 8, 2004, in forum: C++
    Replies:
    12
    Views:
    5,792
    Gary Labowitz
    Jul 9, 2004
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,056
    Dan Pop
    Feb 10, 2004
  3. Andrew
    Replies:
    2
    Views:
    374
    Pete Becker
    Oct 19, 2007
  4. Juha Nieminen
    Replies:
    11
    Views:
    596
    Triple-DES
    Jan 12, 2009
  5. Questionable advice

    , Sep 8, 2013, in forum: C++
    Replies:
    50
    Views:
    643
Loading...

Share This Page