If a cast from void* to a struct type pointer fails

Discussion in 'C Programming' started by William Payne, Feb 13, 2004.

  1. Hello!
    If a cast from a void pointer to a pointer to a struct fails at runtime, is
    NULL the result? I.E, is this code meaningful:

    struct my_struct_t foo* = (struct my_struct_t*)void_pointer;
    // void_pointer is of type void*
    if(foo == NULL)
    {
    printf("Error!\n");
    }

    / William Payne
    William Payne, Feb 13, 2004
    #1
    1. Advertising

  2. On Sat, 14 Feb 2004, William Payne wrote:
    >
    > If a cast from a void pointer to a pointer to a struct fails at
    > runtime, is NULL the result? I.E, is this code meaningful:
    >
    > struct my_struct_t foo* = (struct my_struct_t*)void_pointer;


    First of all, it's not correct, because there are syntax errors
    on it. Fixing the errors, and getting rid of the spurious cast
    (which serves only to shut up the compiler about errors; casts
    almost never solve problems), we have

    | struct my_struct_t *foo = void_pointer;

    If 'void_pointer' is pointing at a 'struct my_struct_t', or is
    a null pointer, then this will work in the obvious fashion.
    Otherwise, this line invokes undefined behavior. For example,

    int i;
    void *p = &i;
    struct my_struct_t *foo = void_pointer;

    This is undefined behavior and can do anything it likes (including
    possibly setting 'foo' to NULL, but more likely setting it to a
    nonsense value, rounding it to a "close-enough" value, or raising
    a bus error).

    struct my_struct_t s;
    void *p = &s;
    struct my_struct_t *foo = void_pointer;

    This is defined behavior, and 'foo' now points to 's'.

    HTH,
    -Arthur
    Arthur J. O'Dwyer, Feb 13, 2004
    #2
    1. Advertising

  3. William Payne

    Artie Gold Guest

    William Payne wrote:
    > Hello!
    > If a cast from a void pointer to a pointer to a struct fails at runtime, is


    What do you mean by `fails'?

    > NULL the result? I.E, is this code meaningful:
    >
    > struct my_struct_t foo* = (struct my_struct_t*)void_pointer;


    Do you perhaps mean:

    struct my_struct_t *foo - (struct my_struct_t *)void_pointer;

    > // void_pointer is of type void*
    > if(foo == NULL)
    > {
    > printf("Error!\n");
    > }


    With the correction above, sure -- iff `void_pointer' was NULL to begin
    with and originated as a pointer to `struct my_struct_t'.

    I think you either have another question here -- or it's time to OTDB!*

    HTH,
    --ag

    * - Open The Damn Book
    --
    Artie Gold -- Austin, Texas

    "Yeah. It's an urban legend. But it's a *great* urban legend!"
    Artie Gold, Feb 13, 2004
    #3
  4. William Payne

    Artie Gold Guest

    Artie Gold wrote:
    > William Payne wrote:
    >
    >> Hello!
    >> If a cast from a void pointer to a pointer to a struct fails at
    >> runtime, is

    >
    >
    > What do you mean by `fails'?
    >
    >> NULL the result? I.E, is this code meaningful:
    >>
    >> struct my_struct_t foo* = (struct my_struct_t*)void_pointer;

    >
    >
    > Do you perhaps mean:
    >
    > struct my_struct_t *foo - (struct my_struct_t *)void_pointer;

    = (of course)
    >
    >> // void_pointer is of type void*
    >> if(foo == NULL)
    >> {
    >> printf("Error!\n");
    >> }

    >
    >
    > With the correction above, sure -- iff `void_pointer' was NULL to begin
    > with and originated as a pointer to `struct my_struct_t'.
    >
    > I think you either have another question here -- or it's time to OTDB!*
    >
    > HTH,
    > --ag
    >
    > * - Open The Damn Book



    --
    Artie Gold -- Austin, Texas

    "Yeah. It's an urban legend. But it's a *great* urban legend!"
    Artie Gold, Feb 14, 2004
    #4
  5. Arthur J. O'Dwyer <> spoke thus:

    >> If a cast from a void pointer to a pointer to a struct fails at
    >> runtime, is NULL the result? I.E, is this code meaningful:
    >>
    >> struct my_struct_t foo* = (struct my_struct_t*)void_pointer;


    > First of all, it's not correct, because there are syntax errors
    > on it. Fixing the errors, and getting rid of the spurious cast
    > (which serves only to shut up the compiler about errors; casts
    > almost never solve problems), we have


    > | struct my_struct_t *foo = void_pointer;


    Is it possible that the OP was thinking in the C++ world, which (IIRC)
    requires the cast of void_pointer? Perhaps he was looking for
    something like dynamic_cast that throws an exception when it "fails"
    at runtime? Just a (probably wrong and definitely OT) thought.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Feb 17, 2004
    #5
    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. Ollej Reemt
    Replies:
    7
    Views:
    525
    Jack Klein
    Apr 22, 2005
  2. Kobu
    Replies:
    3
    Views:
    506
    Kevin Bracey
    Feb 10, 2005
  3. Abhishek
    Replies:
    12
    Views:
    803
    Eric Sosman
    Jan 30, 2006
  4. verec
    Replies:
    5
    Views:
    3,024
    verec
    Aug 16, 2005
  5. Hakirato
    Replies:
    4
    Views:
    877
    Alf P. Steinbach
    Oct 5, 2006
Loading...

Share This Page