convert from size_t to unsigned int

G

Guest

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
 
G

Guest

George said:
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
 
G

Guest

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
 
B

Bo Persson

George wrote:
:: 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
::
:: "docschnipp" wrote:
::
::: "George" wrote:
:::
:::: 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
 
D

David Wilkinson

George said:
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?

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;
 
G

Guest

Bo Persson said:
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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
D

David Wilkinson

George said:
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

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

Guest

George said:
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
 
G

Guest

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
 
D

David Wilkinson

George said:
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).
 
G

Guest

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
 
G

Guest

George said:
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
 
G

Guest

George said:
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
 
G

Guest

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
 
D

David Wilkinson

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

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

Guest

George said:
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
 

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

Top