Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When converting the (long*) value(-4) to __int64 got the value (4294967292), got this error "Value was either too large or too small for an Int32". How to solve this issue?
Posted
Comments
[no name] 2-Apr-14 7:56am    
Code please.
King Fisher 2-Apr-14 8:07am    
show the error line.

__int64 vs. Int32: do you see the difference? a UInt32 would do here, but not an Int32. The maximum number available for Int32 is 2^31-1, and your number is 2^32-4, just a little too big by 2^31-3...
 
Share this answer
 
Easy: don't cast a pointer to an int type! Ever! Or vice versa for that matter. Whatever you think is a good reason for doing just that, you're wrong!
 
Share this answer
 
Comments
Rage 3-Apr-14 5:07am    
Why ? What is wrong with casting ?
Stefan_Lang 3-Apr-14 5:19am    
In C? It depends. In C++? pretty much everything! One of C++s greatest adcantage is type safety. Type casting destroys that feature. That is why - if you really must cast - you should use the new cast operators, not the C-style casts!

But, in any case. I didn't say casting in general, I said casting pointer to int or int to pointer! A pointer is not an int. No discussion! It can't be relied upon to be 4 bytes or 8 bytes or 16 bytes, or , on some odd system maybe 10 or 12 bytes. (or 3 if still you dabble in MS-DOS programming). That alone should give you a hint it's a very, very bad idea!

Moreover, there's absolutely no need to cast a pointer to int or vice versa - ever! Even the MFC - and that is one of the worst C++ APIs I know - nowadays makes sure to differentiate between integer types and pointer types, e. g. by using DWORD_PTR rather than DWORD (and don't think that DWORD_PTR can be safely cast to DWORD - it can't! That is why Microsoft introduced that distinction in the first place!)

P.S.: please note that function pointers may have a different size from, say, int pointers. Just saying...

P.P.S.: In 30 years of programming C++, I've never seen a case where a type cast was really, definitely, necessary, except when dealing with APIs that deliver return values or parameter using another type as the actual object. Show me the context of any type cast and I show you how to avoid it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900