Casting problem ?!

Discussion in 'C++' started by Stranger, Dec 2, 2005.

  1. Stranger

    Stranger Guest

    what is stored in "i" variable in this program ?

    long i;
    float y;
    float number;
    y = number;
    i = * ( long * ) &y;

    notice that "number" variable has been initialized ...
    Stranger, Dec 2, 2005
    #1
    1. Advertising

  2. The first N bytes of y.
    N=sizeof(long)

    The content of the first N bytes depends on how your computer an OS
    represents floats in memory.

    Stranger wrote:
    > what is stored in "i" variable in this program ?
    >
    > long i;
    > float y;
    > float number;
    > y = number;
    > i = * ( long * ) &y;
    >
    > notice that "number" variable has been initialized ...
    >
    Carlos Martinez Garcia, Dec 2, 2005
    #2
    1. Advertising

  3. Stranger wrote:
    > what is stored in "i" variable in this program ?
    >
    > long i;
    > float y;
    > float number;
    > y = number;
    > i = * ( long * ) &y;
    >
    > notice that "number" variable has been initialized ...


    Where is the initialization of "number"? I nothing initialized, and y
    assigned a junk value.
    Also, please use C++-style casts - they are provided for a reason.
    The result would be a junk value in "i".
    Neelesh Bodas, Dec 2, 2005
    #3
  4. Stranger

    Mark Guest

    i *think* he may have meant "note" that "number" has been initialized.
    ie, elsewhere in his program that he has not shown.

    however, i wouldnt know whats in i... why cant you just do
    i = long(y)? or (long)y?
    Mark, Dec 2, 2005
    #4
  5. Stranger

    mlimber Guest

    Mark wrote:
    > i *think* he may have meant "note" that "number" has been initialized.
    > ie, elsewhere in his program that he has not shown.
    >
    > however, i wouldnt know whats in i... why cant you just do
    > i = long(y)? or (long)y?


    Perhaps because that was not the author's intent. If s/he wanted to
    truncate y to the nearest whole number, s/he would have done just what
    you said. If, however, s/he wanted an implementation-dependent,
    bit-wise representation of y, then the nasty pointer casts may
    accomplish that (depending on the sizes of the various data types).
    Using reinterpret_cast would be preferred because it would signal
    non-portable code.

    Cheers! --M
    mlimber, Dec 2, 2005
    #5
  6. Stranger

    Guest

    mlimber wrote:
    > Mark wrote:
    > > i *think* he may have meant "note" that "number" has been initialized.
    > > ie, elsewhere in his program that he has not shown.
    > >
    > > however, i wouldnt know whats in i... why cant you just do
    > > i = long(y)? or (long)y?

    >
    > Perhaps because that was not the author's intent. If s/he wanted to
    > truncate y to the nearest whole number, s/he would have done just what
    > you said. If, however, s/he wanted an implementation-dependent,
    > bit-wise representation of y, then the nasty pointer casts may
    > accomplish that (depending on the sizes of the various data types).
    > Using reinterpret_cast would be preferred because it would signal
    > non-portable code.


    Isn't the OP's code

    float y;
    float number;
    y = number;

    undefined behaviour is number is not initialised before being assigned
    to y?

    Gavin Deane
    , Dec 2, 2005
    #6
  7. Stranger

    Ron Natalie Guest

    Carlos Martinez Garcia wrote:
    > The first N bytes of y.
    > N=sizeof(long)
    >
    > The content of the first N bytes depends on how your computer an OS
    > represents floats in memory.
    >


    It might not be even that. There's no guarantee that float* can
    be converted to long* with a cast.
    Ron Natalie, Dec 4, 2005
    #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. Craig Buchanan

    IIdentity casting problem

    Craig Buchanan, Feb 24, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    987
    Craig Buchanan
    Feb 24, 2004
  2. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Casting Problem

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Aug 17, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    491
    Scott Allen
    Aug 18, 2004
  3. kevin
    Replies:
    11
    Views:
    5,782
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    510
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    549
    James Kanze
    Dec 20, 2009
Loading...

Share This Page