views:

487

answers:

5

All this originated from me poking at a compiler warning message (C4267) when attempting the following line:

const unsigned int nSize = m_vecSomeVec.size();

size() returns a size_t which although typedef'd to unsigned int, is not actually a unsigned int. This I believe have to do with 64 bit portability issues, however can someone explain it a bit better for me? ( I don't just want to disable 64bit warnings.)

+2  A: 

If size_t is typedef:ed to unsigned int, then of course it is an unsigned int, on your particular platform. But it is abstracted so that you cannot depend on it always being an unsigned int, it might be larger on some other platform.

Probably it has not been made larger since it would cost too much to do so, and e.g. vectors with more than 2^32 items in them are not very common.

unwind
+3  A: 

When compiling for a 64-bit platform, size_t will be a 64-bit type. Because of this, Visual Studio gives warnings about assigning size_ts to ints when 'Detect 64-bit Portability Issues' is enabled.

Visual C++ gets this information about size_t through the __w64 token, e.g. __w64 unsigned int.

James Hopkin
A: 

Depending on the compiler, int may be 32-bits in 64-bit land.

MSN

MSN
+8  A: 

It depends on the implementation. std::size_t for example has a minimal required size. But there is no upper limit. To avoid these kind of situations, always use the proper typedef:

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

You will be always on the safe side then.

Johannes Schaub - litb
True - size_t might not be big enough for vector<bool>.
MSalters
A: 

Andrey Karpov. 64 bits, Wp64, Visual Studio 2008, Viva64 and all the rest...

Abstract. The purpose of this article is to answer some questions related to safe port of C/C++ code on 64-bit systems. The article is written as an answer to the topic often discussed on forums and related to the use of /Wp64 key and Viva64 tool.

I appreciate your reply, but this site is a resource, not an advertising tool. If you have something to contribute please do, but please don't use it to peddle your products.
Konrad