Re: dereferencing void *

Discussion in 'C Programming' started by Robert W Hand, Jul 6, 2003.

  1. On 6 Jul 2003 12:19:28 -0700, (maxw_cc) wrote:

    >>You could also do something like this:
    >>
    >> int a = 4;
    >> void* addr = &a;
    >> float b;
    >> b = *((float*)addr);

    <snip>
    >Now about the code you just mentioned, will it be violating some
    >constraint (or semantic constraint) or the only reason it's invalid
    >is because of practical usage (in practice it wouldn't yield valuable
    >or useful result)?


    Hopefully the standard will reflect practical issues and usage. :)

    Subclause 6.5/7 clearly lists what expressions can access objects.
    First of all the expression must be an lvalue. I've copied the exact
    language(*) at the end, but the gist is that the type of the
    expression must be essentially the same as the type of the object or
    be a character type.

    In other words, if an object is of type X, then it can be read by
    expressions of type X, const X, volatile X, char and other character
    types, or for integer types X, signed X, unsigned X, signed const X,
    unsigned const X, etc.

    So you are not allowed to read a float object using an expression of
    type int. It is an example of undefined behavior. It might work out
    and it might not.

    (*)
    — a type compatible with the effective type of the object,
    — a qualified version of a type compatible with the effective type of
    the object,
    — a type that is the signed or unsigned type corresponding to the
    effective type of the object,
    — a type that is the signed or unsigned type corresponding to a
    qualified version of the effective type of the object,
    — an aggregate or union type that includes one of the aforementioned
    types among its members (including, recursively, a member of a
    subaggregate or contained union), or
    — a character type.

    Best wishes,

    Bob
    Robert W Hand, Jul 6, 2003
    #1
    1. Advertising

  2. Robert W Hand

    maxw_cc Guest

    Robert W Hand <> wrote in message news:<>...
    > On 6 Jul 2003 12:19:28 -0700, (maxw_cc) wrote:
    >
    > >>You could also do something like this:
    > >>
    > >> int a = 4;
    > >> void* addr = &a;
    > >> float b;
    > >> b = *((float*)addr);

    > <snip>
    > >Now about the code you just mentioned, will it be violating some
    > >constraint (or semantic constraint) or the only reason it's invalid
    > >is because of practical usage (in practice it wouldn't yield valuable
    > >or useful result)?

    >
    > Hopefully the standard will reflect practical issues and usage. :)
    >
    > Subclause 6.5/7 clearly lists what expressions can access objects.
    > First of all the expression must be an lvalue. I've copied the exact
    > language(*) at the end, but the gist is that the type of the
    > expression must be essentially the same as the type of the object or
    > be a character type.
    >
    > In other words, if an object is of type X, then it can be read by
    > expressions of type X, const X, volatile X, char and other character
    > types, or for integer types X, signed X, unsigned X, signed const X,
    > unsigned const X, etc.
    >
    > So you are not allowed to read a float object using an expression of
    > type int. It is an example of undefined behavior. It might work out
    > and it might not.


    ....[snip]...

    Thank you very much Bob for sharing with us
    your knowledge and valuable information.

    Max
    maxw_cc, Jul 7, 2003
    #2
    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:
    501
    Jack Klein
    Apr 22, 2005
  2. Jun Woong

    Re: dereferencing void *

    Jun Woong, Jul 6, 2003, in forum: C Programming
    Replies:
    0
    Views:
    364
    Jun Woong
    Jul 6, 2003
  3. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    778
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    395
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page