.NET Forum / Languages / Managed C++ / August 2007
convert from size_t to unsigned int
|
|
Thread rating:  |
George - 28 Jul 2007 07:36 GMT Hello everyone,
When converting from size_t to unsigned int, there will be a warning message,
warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
I do not know why, since in crtdbg.h, size_t is defined to int, right?
thanks in advance, George
docschnipp - 28 Jul 2007 09:50 GMT > When converting from size_t to unsigned int, there will be a warning message, > > warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data > > I do not know why, since in crtdbg.h, size_t is defined to int, right? No, it is defined to unsigned int. And you are not converting from size_t to unsigned int, otherwise the warning message wouldn't come up.
best doc
George - 28 Jul 2007 10:24 GMT Hi doc,
I am using VS 2003. I find that even if I convert to unsigned int, the error message is the same.
Do you know the reason?
regards, George
> > When converting from size_t to unsigned int, there will be a warning message, > > [quoted text clipped - 7 lines] > best > doc Bo Persson - 28 Jul 2007 10:32 GMT :: Hi doc, :: :: I am using VS 2003. I find that even if I convert to unsigned int, :: the error message is the same. :: :: Do you know the reason? 1) It is a compatibility warning, because size_t doesn't have to be the same size on all systems. Particularly, for 64 bit compiles it is different from a 32 bit compile.
http://msdn2.microsoft.com/en-us/library/6kck0s93(VS.80).aspx
2) Why do you want to do this in the first place? Why not use a size_t variable?
Bo Persson
:: regards, :: George [quoted text clipped - 18 lines] ::: best ::: doc docschnipp - 28 Jul 2007 10:44 GMT > 2) Why do you want to do this in the first place? Why not use a size_t > variable? I guess the reason is as usual: using several pieces of code, one is using size_t, others, not originating the Windows platform, use int or unsigned int.
best doc
George - 28 Jul 2007 11:10 GMT Hi doc,
I do not know why in Visual Studio 2003, there is warning message when converting from size_t to unsigned int. I think they are the same?
Any ideas?
regards, George
> > 2) Why do you want to do this in the first place? Why not use a size_t > > variable? [quoted text clipped - 5 lines] > best > doc docschnipp - 28 Jul 2007 11:36 GMT > Hi doc, > > I do not know why in Visual Studio 2003, there is warning message when > converting from size_t to unsigned int. I think they are the same? > > Any ideas? Read the link that Bo has supplied. It is a warning message concerning the portability to 64bit code, which would imply that size_t is not 32bit like an int or unsigned int anymore. Even if it is the same in your context, it is possible that it is not when changing the compile target.
You can turn off the compatibility warning if it annoys you or just size_t everywhere in your code (also a preferable option).
best doc
George - 28 Jul 2007 12:24 GMT Thanks doc,
I am using 32-bit platform. Do you think there are any risk of convert size_t to unsigned int, which will occur possible data lost?
regards, George
> > Hi doc, > > [quoted text clipped - 13 lines] > best > doc docschnipp - 28 Jul 2007 13:26 GMT > Thanks doc, > > I am using 32-bit platform. Do you think there are any risk of convert > size_t to unsigned int, which will occur possible data lost? No, the warning is because the compiler internally detects that usage of size_t with unsigned int warns you about a possible 64bit porting issue. Again, I suggest intense reading of the link Bo supplied. You will not have a problem as long you keep it Win32, but why use an unsigned int when you can use size_t?
To quote some old termN. "It is not a bug, it is a feature."
have a nice day doc
George - 29 Jul 2007 07:04 GMT Thanks doc,
I have understood your points of making future compatbility for 64-bit platform. I will follow your points.
Have a good weekend.
regards, George
> > Thanks doc, > > [quoted text clipped - 11 lines] > have a nice day > doc Ben Voigt [C++ MVP] - 02 Aug 2007 14:13 GMT >> 2) Why do you want to do this in the first place? Why not use a size_t >> variable? > > I guess the reason is as usual: using several pieces of code, one is using > size_t, others, not originating the Windows platform, use int or unsigned > int. size_t is standard, not Windows-specific
> best > doc David Wilkinson - 02 Aug 2007 14:52 GMT > size_t is standard, not Windows-specific Ben:
Yes, but using int for indexing is not specific to libraries intended for Windows. For example, I use Numerical Recipes a lot, and their vector/matrix classes use int for indexing.
 Signature David Wilkinson Visual C++ MVP
George - 03 Aug 2007 08:32 GMT Thanks for your advice, Ben.
regards, George
> >> 2) Why do you want to do this in the first place? Why not use a size_t > >> variable? [quoted text clipped - 7 lines] > > best > > doc George - 28 Jul 2007 11:08 GMT Hi,
I am using 32-bit machine. I need to pass the size_t to another function which accepts unsigned int.
I think it is ok to pass size_t to unsigned int, but I do not know why there is warning.
regards, George
> :: Hi doc, > :: [quoted text clipped - 36 lines] > ::: best > ::: doc David Wilkinson - 28 Jul 2007 10:41 GMT > Hello everyone, > [quoted text clipped - 3 lines] > > I do not know why, since in crtdbg.h, size_t is defined to int, right? George:
No, In Win32 size_t is unsigned int. The error message says size_t to int (not size_t to unsigned int as in your question).
In Win64 (or with Win64 warnings turned on), you will get a warning for converting size_t to unsigned int, because in Win64 size_t is an unsigned 64-bit value.
Just use an explicit cast. Although I use C++ casts in other contexts, for simple integer conversions I normally use C-style cast:
size_t n1 = 12345; int n2 = (int)n1;
 Signature David Wilkinson Visual C++ MVP
George - 28 Jul 2007 11:10 GMT Hi David,
When converting from size_t to unsigned int, there are still warning message in Visual Studio 2003. You can have a try. I have no idea why converting to unsigned int has warning information.
regards, George
> > Hello everyone, > > [quoted text clipped - 18 lines] > size_t n1 = 12345; > int n2 = (int)n1; David Wilkinson - 28 Jul 2007 11:34 GMT > Hi David, > [quoted text clipped - 26 lines] >> size_t n1 = 12345; >> int n2 = (int)n1; George:
Maybe you have 64-bit warnings turned on (a good idea, in my opinion). Look in project properties under
Configuration Properties -> C/C++ -> General.
 Signature David Wilkinson Visual C++ MVP
George - 28 Jul 2007 12:24 GMT Thanks David,
Which macro indicates I have 64-bit warnings turned on?
regards, George
> > Hi David, > > [quoted text clipped - 33 lines] > > Configuration Properties -> C/C++ -> General. David Wilkinson - 28 Jul 2007 12:39 GMT > Thanks David, > > Which macro indicates I have 64-bit warnings turned on? George:
I just cannot believe that you do not have enough information from this thread to solve any problem you might be having. The purpose of the newsgroups is not to do your work for you, but rather to point you in the right direction.
Also, you are really in the wrong newsgroup. The correct group for questions on standard C++ is
microsoft.public.vc.language
This group (microsoft.public.dotnet.languages.vc) is intended for .NET programming using C++ (managed C++ or C++/CLI).
 Signature David Wilkinson Visual C++ MVP
George - 28 Jul 2007 13:12 GMT Thanks David,
I will go to the newsgroup to discuss. I am not going to rely on others and do not do any work by myself. Acutally, I want to learn others' points why converting from size_t to unsigned int will cause warning message -- is it a defect in Visual Studio?
I have looked into winnt.h that size_t is actually defined to unsigned int. This is why I have such question.
regards, George
> > Thanks David, > > [quoted text clipped - 14 lines] > This group (microsoft.public.dotnet.languages.vc) is intended for .NET > programming using C++ (managed C++ or C++/CLI). docschnipp - 28 Jul 2007 13:28 GMT > Thanks David, > > I will go to the newsgroup to discuss. I am not going to rely on others and > do not do any work by myself. Acutally, I want to learn others' points why > converting from size_t to unsigned int will cause warning message -- is it a > defect in Visual Studio? George,
why don't you just read that link. It is a FEATURE of Visual Studio warning you that your code is not very clean at that certain point and WILL cause problems when compiling for 64bit. Never think you will stay on Win32 at all times.
Discussing the same topic in another newsgroup will not come up with something new and you will get the same answers and pointers. You need to read and inspect the information that is already given to you, honestly.
doc
George - 28 Jul 2007 14:26 GMT Thanks for your advice, doc.
I can understand your points. But please believe me that I am using 32-bit only -- no 64-bit platform.
I think there may be some options which I mis-set to make it 64-bit so that compiler has the warning message. Could you let me know how to check please?
regards, George
> > Thanks David, > > [quoted text clipped - 15 lines] > > doc docschnipp - 28 Jul 2007 19:26 GMT > Thanks for your advice, doc. > > I can understand your points. But please believe me that I am using 32-bit > only -- no 64-bit platform. Think of your words in 2 years :)
> I think there may be some options which I mis-set to make it 64-bit so that > compiler has the warning message. Could you let me know how to check please? Go to your project properties, Configuration Properties, C/C++ and turn off the option "Detect 64-bit Portability Issues".
doc
George - 29 Jul 2007 07:00 GMT Thanks doc,
I have found the option.
regards, George
> > Thanks for your advice, doc. > > [quoted text clipped - 10 lines] > > doc David Wilkinson - 28 Jul 2007 14:29 GMT > Thanks David, > [quoted text clipped - 5 lines] > I have looked into winnt.h that size_t is actually defined to unsigned int. > This is why I have such question. George:
As several people have pointed out to you, this must be a 64-bit portability warning. It is not a defect in Visual Studio. You can turn off 64-bit warnings as I described in this thread. The warnings come because on Win64 there are size_t values that cannot be represented in 32 bits (because they are too big). This is very rarely a problem in practice. But the best thing, if possible, is to write your code so that it does not require conversions between integer types. Sometimes this is not possible, such as when your code mixes CString (which uses int's) and std::string (which uses size_t), in which case you have to cast.
I notice that you have posted another question. This new question is about interoperability between C++ and C#. This *is* a .NET question, and properly belongs in this group. However you have posted it both in this group, and in microsoft.public.vc.language. This is called multi-posting, and is a very bad practice, because the replies get fragmented (as you will discover). Better than multi-posting is cross-posting (if your software allows it); this is posting the same message simultaneously to two or more groups.
But by far the best method is to pick the single most appropriate group and just post there. If the question is about .NET (managed code) then post here; otherwise use microsoft.public.vc.language.
 Signature David Wilkinson Visual C++ MVP
George - 29 Jul 2007 07:02 GMT Thanks for your advice, David.
I will convert the function prototype from unsigned int to size_t for future compatibility consideration.
regards, George
> > Thanks David, > > [quoted text clipped - 30 lines] > and just post there. If the question is about .NET (managed code) then > post here; otherwise use microsoft.public.vc.language.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|