example rquired

Discussion in 'C Programming' started by siju, Oct 21, 2007.

  1. siju

    siju Guest

    Hi all,
    the following post was saying that
    http://groups.google.com/group/comp.lang.c/search?q=aarklon&start=30&sa=N&

    The language doesn't allow conversions from floating-point to poitner
    types, either implicitly or explicitly.

    > What?


    > float x = 1.0f;
    > void *p = *((void**)&x);


    This piece of code does not perform a _conversion_ of floating-point
    value to pointer type. What you have here is a _reinterpretation_ of
    floating-point lvalue as pointer lvalue (raw memory reinterpretation).
    This is a completely different thing in C language world.

    For example, in C language _converting_ a pointer of type 'T*' to
    'void*' type and back is defined and has its uses. However, trying to
    _reinterpret_ a pointer of type 'T*' as a 'void*' pointer (using the
    above technique) leads to undefined results. Feel the difference.

    now my questions are

    1) in C language _converting_ a pointer of type 'T*' to
    'void*' type and back is defined and has its uses.

    can anyone give examples especially on uses???

    2) However, trying to _reinterpret_ a pointer of type 'T*' as a
    'void*' pointer (using the above technique) leads to undefined
    results.

    explanation on un defined results ??
    siju, Oct 21, 2007
    #1
    1. Advertising

  2. siju

    Jack Klein Guest

    On Sun, 21 Oct 2007 03:21:53 -0000, siju <> wrote in
    comp.lang.c:

    >
    > Hi all,
    > the following post was saying that
    > http://groups.google.com/group/comp.lang.c/search?q=aarklon&start=30&sa=N&
    >
    > The language doesn't allow conversions from floating-point to poitner
    > types, either implicitly or explicitly.


    That is almost correct. The C standard allows the conversion, with a
    suitable cast, between pointers and integer types. Conversion of a
    pointer to an integer has implementation-defined behavior if the
    result of the conversion fits in the destination integer type,
    undefined behavior otherwise.

    If an implementation has an integer type capable of holding a pointer
    value, conversion of a pointer value to this integer type, via a cast,
    and then back again to a pointer to the original type, again via a
    cast, will result in a pointer that points to the same object.

    But you can convert from a floating point type to a pointer type using
    two casts, of course.

    double d = 3.14159;
    void *vp = (void *)((int)d);

    So if you want to be completely correct, you would have to say that
    you can't directly convert between floating point and pointer types,
    implicitly or explicitly.

    > > What?

    >
    > > float x = 1.0f;
    > > void *p = *((void**)&x);

    >
    > This piece of code does not perform a _conversion_ of floating-point
    > value to pointer type. What you have here is a _reinterpretation_ of
    > floating-point lvalue as pointer lvalue (raw memory reinterpretation).
    > This is a completely different thing in C language world.


    What you have is undefined behavior, pure and simple. It is invalid
    to access the object representation of a double as anything other than
    a double, or as an array of character types the size of a double. The
    later is only truly safe if the character type used is unsigned char.

    > For example, in C language _converting_ a pointer of type 'T*' to
    > 'void*' type and back is defined and has its uses. However, trying to
    > _reinterpret_ a pointer of type 'T*' as a 'void*' pointer (using the
    > above technique) leads to undefined results. Feel the difference.
    >
    > now my questions are
    >
    > 1) in C language _converting_ a pointer of type 'T*' to
    > 'void*' type and back is defined and has its uses.


    That is only true if T is a complete or incomplete object type. It is
    not true if T is a function type. There is no defined conversion
    between function pointers and object pointers.

    > can anyone give examples especially on uses???


    Look up functions such as malloc(), free(), and qsort() in your C
    reference.

    > 2) However, trying to _reinterpret_ a pointer of type 'T*' as a
    > 'void*' pointer (using the above technique) leads to undefined
    > results.


    There are only certain ways that one is allowed to access the value of
    an object in C to have guaranteed defined results.

    1. Access as the type of the object, or a compatible type.

    2. Access as an array of unsigned chars.

    It just so happens that C requires that pointer to char and pointer to
    void have the same type and representation, so you can access the
    value of a pointer to char as a pointer to void and vice versa.

    > explanation on un defined results ??


    Type "C undefined behavior" into Google and look at the first result.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 21, 2007
    #2
    1. Advertising

  3. "siju" <> wrote in message
    > 1) in C language _converting_ a pointer of type 'T*' to
    > 'void*' type and back is defined and has its uses.
    >
    > can anyone give examples especially on uses???
    >

    Look up qsort(). It uses void *s, and also shows why void*s are necessary.
    >
    > 2) However, trying to _reinterpret_ a pointer of type 'T*' as a
    > 'void*' pointer (using the above technique) leads to undefined
    > results.
    >
    > explanation on un defined results ??
    >

    This is a subtle one.

    int *ptr;
    void *vptr;

    /* bad */
    memcpy(&ptr, &vptr, sizeof(void *));
    /* good */
    ptr = vptr;

    The reason is that void*s might carry about extra baggage. For instance some
    machines have a 32-byte architecture, but implent 8 bit chars on top of it.
    So char *s have an extra two bits to indicate offset. void *s obvious need
    these extra two bits, because they can be converted to char *s and back.
    So sizeof(int *) might not equal sizeof(void *).


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Oct 21, 2007
    #3
  4. Jack Klein <> writes:
    > On Sun, 21 Oct 2007 03:21:53 -0000, siju <> wrote in
    > comp.lang.c:
    >> the following post was saying that
    >> http://groups.google.com/group/comp.lang.c/search?q=aarklon&start=30&sa=N&
    >>
    >> The language doesn't allow conversions from floating-point to poitner
    >> types, either implicitly or explicitly.

    >
    > That is almost correct. The C standard allows the conversion, with a
    > suitable cast, between pointers and integer types. Conversion of a
    > pointer to an integer has implementation-defined behavior if the
    > result of the conversion fits in the destination integer type,
    > undefined behavior otherwise.
    >
    > If an implementation has an integer type capable of holding a pointer
    > value, conversion of a pointer value to this integer type, via a cast,
    > and then back again to a pointer to the original type, again via a
    > cast, will result in a pointer that points to the same object.
    >
    > But you can convert from a floating point type to a pointer type using
    > two casts, of course.
    >
    > double d = 3.14159;
    > void *vp = (void *)((int)d);
    >
    > So if you want to be completely correct, you would have to say that
    > you can't directly convert between floating point and pointer types,
    > implicitly or explicitly.

    [...]

    Are you sure about that? What constraint does it violate?

    C99 6.5.4, "Cast operators", constrains the source and target types to
    be scalar (unless the target is void). C99 6.3, "Conversions",
    doesn't define the behavior of conversions between floating-point and
    pointer types, but there are no constraints in that section.

    As far as I can tell, conversion between a pointer type and a
    floating-point type is legal, but the behavior is undefined. (IMHO it
    would make more sense if it were illegal, but the standard doesn't
    seem to say so.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 21, 2007
    #4
  5. siju

    Jack Klein Guest

    On Sun, 21 Oct 2007 00:47:00 -0700, Keith Thompson <>
    wrote in comp.lang.c:

    > Jack Klein <> writes:
    > > On Sun, 21 Oct 2007 03:21:53 -0000, siju <> wrote in
    > > comp.lang.c:
    > >> the following post was saying that
    > >> http://groups.google.com/group/comp.lang.c/search?q=aarklon&start=30&sa=N&
    > >>
    > >> The language doesn't allow conversions from floating-point to poitner
    > >> types, either implicitly or explicitly.

    > >
    > > That is almost correct. The C standard allows the conversion, with a
    > > suitable cast, between pointers and integer types. Conversion of a
    > > pointer to an integer has implementation-defined behavior if the
    > > result of the conversion fits in the destination integer type,
    > > undefined behavior otherwise.
    > >
    > > If an implementation has an integer type capable of holding a pointer
    > > value, conversion of a pointer value to this integer type, via a cast,
    > > and then back again to a pointer to the original type, again via a
    > > cast, will result in a pointer that points to the same object.
    > >
    > > But you can convert from a floating point type to a pointer type using
    > > two casts, of course.
    > >
    > > double d = 3.14159;
    > > void *vp = (void *)((int)d);
    > >
    > > So if you want to be completely correct, you would have to say that
    > > you can't directly convert between floating point and pointer types,
    > > implicitly or explicitly.

    > [...]
    >
    > Are you sure about that? What constraint does it violate?


    I don't believe that I said it violated a constraint. Admittedly,
    "can't" could mean either a constraint violation or undefined
    behavior, and I did not specify which, but either one is good for
    "can't" in my view. In any cast, I meant undefined behavior, and not
    a constraint violation.

    > C99 6.5.4, "Cast operators", constrains the source and target types to
    > be scalar (unless the target is void). C99 6.3, "Conversions",
    > doesn't define the behavior of conversions between floating-point and
    > pointer types, but there are no constraints in that section.
    >
    > As far as I can tell, conversion between a pointer type and a
    > floating-point type is legal, but the behavior is undefined. (IMHO it
    > would make more sense if it were illegal, but the standard doesn't
    > seem to say so.)


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 22, 2007
    #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. Bright
    Replies:
    1
    Views:
    540
  2. Timo Dammes
    Replies:
    2
    Views:
    6,514
    Elliot Schei
    Jun 11, 2004
  3. RA
    Replies:
    3
    Views:
    565
    Mike Moore [MSFT]
    Oct 16, 2003
  4. rquired help

    , Nov 1, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    682
  5. Sam Roberts
    Replies:
    15
    Views:
    289
    Sam Roberts
    Feb 7, 2005
Loading...

Share This Page