C4305 error (__int64 and size_t)

Hello,

I have recently come across a problem in my program, the worst part is that it is my last error and I have no idea what it takes to fix it.

I was given the following two warnings:
[...] : warning C4305: 'argument' : truncation from '__int64' to 'size_t'
[...] : warning C4309: 'argument' : truncation of constant value


All that I have been able to find is that it relates to the OS system (Yay! I found out a way of knowing if I'm running 32 or 64 bit....), and yes I am running 64.

Now I imagine the code is also needed so here are the key points. I would prefer that no one recommend changes that are not pertinent to the problem, I am here only for this.

1
2
3
4
size_t main()
{
	List myList;
	myList.concatenate(524321654987); // this is the line where the error occurs 


the function header is found in a .h file, and implementation in another .cpp

 
	void concatenate(size_t num);


1
2
void List::concatenate(size_t num)
{...}


The number that is used works fine until you hit 7 digits, after that it simply gives the error. I have tried to input the number via cin, in that case there is no error message, but the program still runs with the truncated version. (ex: 321654987 becomes 3 654 987).

Thank you for any help on the subject.
Last edited on
The number is too big for size_t. Use __int64 instead:
 
void List::concatenate(__int64 num)
__int64 is a VS thing only. For compatibility purposes, I wouldn't recommend using it directly.


I don't know if there's a standard for 64-bit data types yet. long long is probably your best bet, but I don't think older versions of VS like it.

The best way to go is probably to typedef it:

1
2
3
4
// in a header file somewhere
typedef long long int64;

// now use int64 instead of __int64 or long long 


This way if a specific compiler has problems with the type you can just change the typedef.
Ok, after changing almost all size_t s in my program, it mostly works (1 last bug but I imagine I can figure that one out, must be a size_t left somewhere).

However, I don't understand very well why it works like so. I was told that size_t had no limit to its size... Does __int64 have a limit as well then? If so, what is/are it/them?
all built-in types have limits.

__int64's limit [-263 .. 263-1]

You can always find the limits with <limits>

1
2
3
4
5
6
#include <limits>

int main()
{
cout << std::numeric_limits< __int64 >::max();
}
Disch, sorry I had not seen the response (posted same time).

I was just wondering what would occur if I left a 64 bit machine with the program, your solution fixes that perfectly; thank you.

Since the problem is now resolved I'll add just a little something:

Thank you Null for the __int64 trick, it is always good to learn new things and that is one I won't forget.

Thank you Disch for both your answers (although you made me loose some time because I was having fun looking at all of the type limits...^^').
Topic archived. No new replies allowed.