64 bit portability issues

This all came from me trying to squeeze out a compiler warning (C4267) when trying to execute the following line:

const unsigned int nSize = m_vecSomeVec.size();

      

size()

returns size_t, which, although typedef'd for unsigned int, is not actually unsigned int. I suppose this is due to 64-bit portability issues, however can someone please explain me a little better for me? (I don't just want to disable 64-bit warnings.)

+1


source to share


4 answers


It depends on the implementation. std::size_t

, for example, has the minimum required size. But there is no upper limit. To avoid such situations, always use the correct typedef:

const std::vector<T>::size_type nSize = m_vecSomeVec.size();

      



Then you will always be safe.

+8


source


When compiled for a 64-bit platform, it size_t

will be 64-bit. Because of this, Visual Studio gives warnings about the assignment size_t

to to int

when Detect 64-bit Portability Issues is enabled.

Visual C ++ gets this information about size_t

through a token __w64

, eg. __w64 unsigned int

...



Refer to the link below for more information on 64-bit port issues. http://www.viva64.com/en/a/0065/

+3


source


If size_t

is typedef: ed before unsigned int

, then of course it's unsigned int

on your specific platform. But it is abstracted so that you cannot depend on it all the time unsigned int

, it can be more on some other platform.

It probably wasn't zoomed in as it would cost too much to do this, and for example vectors with more than 2 ^ 32 elements in them are not very common.

+2


source


Depending on the compiler it int

can be 32-bit in 64-bit country.

+1


source







All Articles