473,569 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird conversion warning

Take this code:
1: int main()
2: {
3: unsigned short x[5] = { 1, 2, 3, 4, 5 };
4: unsigned short sum = 0;
5:
6: for (int i = 0; i < 5; ++i) {
7: sum += x[i];
8: }
9: }
CL version 13.10.3077 gives the following warning:

err.cpp(7) : warning C4244: '+=' : conversion from 'int' to 'unsigned
short', possible loss of data

Is this correct? Which possible loss of data the compiler is seeing here?


Nov 17 '05 #1
1 1342
Josué Andrade Gomes wrote:
Take this code:
1: int main()
2: {
3: unsigned short x[5] = { 1, 2, 3, 4, 5 };
4: unsigned short sum = 0;
5:
6: for (int i = 0; i < 5; ++i) {
7: sum += x[i];
8: }
9: }
CL version 13.10.3077 gives the following warning:

err.cpp(7) : warning C4244: '+=' : conversion from 'int' to 'unsigned
short', possible loss of data

Is this correct? Which possible loss of data the compiler is seeing here?


C++ compilers implicitly convert your unsigned short type into int for
the addition operation ('+='). Generally speaking it's not a good idea
to use types such as unsigned short, unless there's a tremendous amount
of data, and you want to save memory and fetch time. However, for simple
variables such as 'sum' and 'i' you should use int. The processor is not
able to work with 16-bit or 8-bit numbers efficiently, the ALU can only
work with 32-bit numbers. Every time you use an unsigned short, you have
to know that the compiler will translate it into int every time you do
anything (except assignment) to them.

The warning is there because your code is compiled to something like this:

for(int i = 0; i < 5; ++i)
{
int temp_x = x[i];
int temp_sum = sum;
temp_sum += temp_x;
sum = temp_sum; // possible loss of data here
}

This is much less efficient than if you declared sum int or unsigned.

Tom
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2344
by: Randy Jackson | last post by:
Hello everyone. First of all, apologies in advance if this is a question that gets asked all the time. I tried to search, but wasn't really sure exactly what to search for. Anyway, here's the issue. I've written a simple page in PHP. It displays just fine in Netscape, but for some reason the screen is absolutely blank in Internet...
1
2772
by: amit | last post by:
I am trying to compile the sample program genwin.sqc, using nsqlprep which is used to precompile embedded sql in C. I am getting weird errors and that is because windows.h is included in the genwin.sqc file. I am using Setting the lib and include path: set INCLUDE=C:\Program Files\Microsoft SQL...
10
2153
by: Skybuck Flying | last post by:
This is the source code for nrand48 from gnuwin32 libgw32c long int nrand48 (xsubi) unsigned short int xsubi; { long int result; (void) __nrand48_r (xsubi, &__libc_drand48_data, &result); return result; }
3
4442
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while calling the constructor 'MyString::MyString(const wchar_t *)' c:\SrNet\jury\JuryTest.h(21) : see declaration of 'MyString::MyString' The class...
6
2660
by: Gilles Rochefort | last post by:
Hello, I wrote the following code to wrap some existing C functions, and I get some warning about converting float to int. float zfactor; int tmp_x, corners_x; std::transform(tmp_x,tmp_x+4,corners_x, bind2nd( multiplies<float>(),zfactor) );
2
2008
by: chauhan.alok | last post by:
hi I am a beginner to vc++. I am opening a file("c:\Mydoc.doc") and reading data from it and after that i just want to write data of 1.2 MB part from first file("c:\Mydoc.doc") to New file("c:\Newdoc1.doc") and remaining to other file("c;\Newdoc2.doc"). Code is Giving warning . Pls Help me How these waring can be removed? Code is:
11
3153
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t, int16_t etc. My questions are: 1. Should the conversion macros be used even when assigning from a variable of the same type?
8
1615
by: Roman Mashak | last post by:
Hello, this piece of code, compiled with gcc-3.4.4 (-ansi -pedantic -W -Wall), doesn't generate any warnings regarding conversion from 'double' to 'unsigned int'. Is it normal or a bug of compiler? #include <stdlib.h> #include <stdio.h> void f1(unsigned int j)
9
26323
by: Eric | last post by:
I am working on a large, old code base and attempting to move it to GCC 4.2. Throughout the code, there is stuff like: char *aVar = "aString"; or void aFunc( char *aVar) { ... } aFunc( "aString" );
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.