About possible data lose warning

Discussion in 'C++' started by Vinodh Kumar P, Dec 8, 2004.

  1. int main()
    {
    int aNormalInt = 0;
    short aShortInt = 0;
    aShortInt = aNormalInt; // Why I am not getting a warining for possible
    data lose in MS VC++6.0, even with warning level 4?
    return 0;
    }

    -Vinodh
    Vinodh Kumar P, Dec 8, 2004
    #1
    1. Advertising

  2. "Vinodh Kumar P" <> wrote...
    > int main()
    > {
    > int aNormalInt = 0;
    > short aShortInt = 0;
    > aShortInt = aNormalInt; // Why I am not getting a warining for possible
    > data lose in MS VC++6.0, even with warning level 4?
    > return 0;
    > }


    Could be the compiler sees that you're doing nothing, the program is
    essentially

    int main()
    {
    return 0;
    }

    Ask in microsoft.public.vc.language, they know more about specifics of
    the Visual C++ compiler.

    V
    Victor Bazarov, Dec 8, 2004
    #2
    1. Advertising

  3. Vinodh Kumar P

    Razzer Guest

    This should really be in a VC newsgroup.

    <OT>However, the warning is signalling that that data in aNormalInt is
    truncated in order to fit the aShortInt, so you could lose a few bits
    in the process.</OT>
    Razzer, Dec 8, 2004
    #3
  4. Vinodh Kumar P

    Thrillhouse Guest

    C is more of a user-driven language, one that allows the programmer to do
    things they may not be able to do in a more restricting language, like
    Java. A Java compiler may warn you about data conversion but most C
    compilers will assume you meant to do it, and allow it.
    Thrillhouse, Dec 8, 2004
    #4
  5. Victor Bazarov wrote:

    >"Vinodh Kumar P" <> wrote...
    >
    >
    >>int main()
    >>{
    >> int aNormalInt = 0;
    >> short aShortInt = 0;
    >> aShortInt = aNormalInt; // Why I am not getting a warining for possible
    >>data lose in MS VC++6.0, even with warning level 4?
    >> return 0;
    >>}
    >>
    >>

    >
    >Could be the compiler sees that you're doing nothing, the program is
    >essentially
    >
    > int main()
    > {
    > return 0;
    > }
    >
    >

    Compiler is not giving warning because the above code is semantically valid.
    Semantic error has nothing to do with the optimization. Read section
    4.7 Integral conversions of C++ standard.


    "1 An rvalue of an integer type can be converted to an rvalue of another
    integer type. An rvalue of an enumeration type can be converted to an
    rvalue of an integer type."

    You will get similar 'verse' from c89/c99 standard.

    Krishanu
    Krishanu Debnath, Dec 8, 2004
    #5
  6. Krishanu Debnath wrote:
    > Victor Bazarov wrote:
    >
    >
    >>"Vinodh Kumar P" <> wrote...
    >>
    >>
    >>
    >>>int main()
    >>>{
    >>> int aNormalInt = 0;
    >>> short aShortInt = 0;
    >>> aShortInt = aNormalInt; // Why I am not getting a warining for possible
    >>>data lose in MS VC++6.0, even with warning level 4?
    >>> return 0;
    >>>}
    >>>
    >>>

    >>
    >>Could be the compiler sees that you're doing nothing, the program is
    >>essentially
    >>
    >> int main()
    >> {
    >> return 0;
    >> }
    >>
    >>

    >
    > Compiler is not giving warning because the above code is semantically valid.
    > Semantic error has nothing to do with the optimization. Read section
    > 4.7 Integral conversions of C++ standard.
    >
    >
    > "1 An rvalue of an integer type can be converted to an rvalue of another
    > integer type. An rvalue of an enumeration type can be converted to an
    > rvalue of an integer type."
    >
    > You will get similar 'verse' from c89/c99 standard.


    There is no way to explain a particular compiler's behaviour by quoting
    the Standard, especially when warnings are concerned. Warnings (and
    a great multitude of them) are often not required but still emitted by
    all compilers in one situation or another.

    V
    Victor Bazarov, Dec 8, 2004
    #6
  7. Razzer wrote:
    > This should really be in a VC newsgroup.
    >
    > <OT>However, the warning is signalling that that data in aNormalInt is
    > truncated in order to fit the aShortInt, so you could lose a few bits
    > in the process.</OT>


    Yes, such warning _would_ be signaling, and that's what the OP was
    asking about. The OP was concerned that _no_warning_ was issued (reread
    the original post).
    Victor Bazarov, Dec 8, 2004
    #7
  8. Vinodh Kumar P

    Razzer Guest

    >(reread
    the original post).

    Whoops. I can't read very well, I guess.
    Razzer, Dec 8, 2004
    #8
  9. Vinodh Kumar P wrote:
    > aShortInt = aNormalInt; // Why I am not getting a warining for
    > possible
    > data lose in MS VC++6.0, even with warning level 4?


    .... because, as I remember, on VC6

    sizeof(aShortInt)
    == sizeof(aNormalInt)
    == sizeof(aLongInt)
    == 4.

    All 32-bit...

    Greetings,

    Thomas Ritter
    Thomas Ritter, Dec 9, 2004
    #9
  10. Thomas Ritter wrote:
    > Vinodh Kumar P wrote:
    >
    >> aShortInt = aNormalInt; // Why I am not getting a warining for
    >> possible
    >>data lose in MS VC++6.0, even with warning level 4?

    >
    >
    > ... because, as I remember, on VC6
    >
    > sizeof(aShortInt)
    > == sizeof(aNormalInt)
    > == sizeof(aLongInt)
    > == 4.
    >
    > All 32-bit...


    You remember incorrectly. sizeof(short) is 2 in VC++ v6.

    V
    Victor Bazarov, Dec 9, 2004
    #10
    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. =?Utf-8?B?d2lsYnVyODhAY29tbXVuaXR5Lm5vc3BhbQ==?=

    session state variables across asp.net web apps... We lose them!

    =?Utf-8?B?d2lsYnVyODhAY29tbXVuaXR5Lm5vc3BhbQ==?=, Mar 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,899
    =?Utf-8?B?d2lsYnVyODhAY29tbXVuaXR5Lm5vc3BhbQ==?=
    Mar 9, 2005
  2. MS News \(MS ILM\)
    Replies:
    1
    Views:
    286
    MS News \(MS ILM\)
    Aug 22, 2003
  3. Ady

    Lose position in datagrid

    Ady, Nov 13, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    531
    Guest
    Nov 14, 2003
  4. Amir

    Viewstate lose data

    Amir, Dec 28, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    108
  5. bthumber

    FileUpload Control lose data with any postback

    bthumber, Jul 16, 2010, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    1,599
    bthumber
    Jul 16, 2010
Loading...

Share This Page