Konrad
Konrad

Reputation: 40947

64 bit portability issues

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

Upvotes: 1

Views: 1721

Answers (4)

MSN
MSN

Reputation: 54604

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

Upvotes: 1

James Hopkin
James Hopkin

Reputation: 13973

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.

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

Upvotes: 3

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506965

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.

Upvotes: 8

unwind
unwind

Reputation: 399823

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.

Upvotes: 2

Related Questions