About possible data lose warning

V

Vinodh Kumar P

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
 
V

Victor Bazarov

Vinodh Kumar P said:
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
 
R

Razzer

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>
 
T

Thrillhouse

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.
 
K

Krishanu Debnath

Victor said:
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
 
V

Victor Bazarov

Krishanu said:
Victor Bazarov wrote:



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
 
V

Victor Bazarov

Razzer said:
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).
 
T

Thomas Ritter

Vinodh said:
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
 
V

Victor Bazarov

Thomas said:
... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top