[544]=>Good morning
 [543]=>You know the fee...
 [542]=>Date more, care ...
 [541]=>Moving On



June 2005
sun mon tue wed thu fri sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    


 [RAndoMness]=> 28Sep09
 [JPsDocs] => 22Feb09
 [JPics] => 10Dec11

recent music
Boycott SONY


getentries(06Dec04);   getentries(07Dec04);


conversion from 'size_t' to 'int' (C++ warning)
added Wed June 22 2005 at 9:22 AM
I got an interesting question from a friend/co-worker today that I figured I'd throw to the winds of Google. This is only relevant if you're programming in C++ and requires basic knowledge of such. I cannot provide any gaurentee that the information is useful or even accurate, but I would greatly appreciate if any errors are brought to my attention.

The original problem is a warning when using the vector::size() method to return the current size of a vector. The code snippet below:
	int num = objects.size()

generates the following warning in Visual Studio .NET:
warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

My reply after researching a bit was:

Okay, it looks like the problem here is that objects.size() doesn't technically return an int.
	size_type size( ) const;

This is an example of what I was talking about earlier [This is referring to an email I sent him yesterday explaining why code "breaks" when moving from Visual Studio 6.0 to Visual Studio .NET] that could potentially bite you 5 years from now, even though it's rather harmless right now. I think that size_type is actually an unsigned int or something like that, but the important thing here is that we have to assume that the exact architecture of size_type could be either system dependant, or could change with the next release of the vector library. The best way to fix it probably depends on what you're wanting to do with int num. If you're just displaying num and then discarding it, you should probably declare it as
	vector <var_type>::size_type num;
//where var_type is replaced with the type of vector you have.

If you're wanting to insert num into an array of int's or do several comparisons using it, then explicitly cast the size() as an int:
	int num = (int) objects.size();

This tells the compiler that you do actually know that you're casting and that you may potentially lose data (if the size ends up being larger than an int can hold, namely 2^32/2-1 or 2,147,483,647, num will become bogus.) Technically speaking, this could be a problem if, say, you were filling a vector with data for each person in the world. In practice, though, I doubt you're building vectors of that size. If you are, you probably should be using something lighter weight than a vector.

If you're using num in a for loop, it's up to you whether you want to do something like:
	for(vector <var_type>::size_type i=0; i<objects.size();i++){}

	for(int i=0; i<(int) objects.size(); i++){}

Personally, I'd probably just caste it as an int. Let me know if this works out for you or if you have any more questions.