dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] Error Int

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    Resolved [RESOLVED] Error Int

    Code:
    bool MaxZen( char * ini )
    {
    	DWORD Money;
    	Money=GetPrivateProfileInt("Server","MaxInventoryZen",20000000000,ini);
    	*(unsigned int*) GS_MAX_ZEN=Money;	
    
    	Money=GetPrivateProfileInt("Server","MaxVaultZen",20000000000,ini);
    	*(unsigned int*) GS_MAX_ZEN1=Money;
    	*(unsigned int*) GS_MAX_ZEN2=Money;
    	*(unsigned int*) GS_MAX_ZEN3=Money;
    	return 1;
    }
    and got error:
    warning C4305: 'argument' : truncation from '__int64' to 'INT' for Money

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    652

    Re: Error Int

    Are you compiling for 32 or 64 bit?

    GetPrivateProfileInt() returns a type UINT. Why are using DWORD for Money?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Error Int

    Hi diablo21,
    Warnings are not the same as errors but in this case it does appear to reveal one.
    The default value you are passing to GetPrivateProfileInt, 20000000000, is too large to represent with a variable of type INT. An overflow occurs where the actual value passed becomes -1474836480 (2820130816 unsigned) which is probably not what you expect to happen.
    The maximum value that an unsigned int can represent is 4294967295 and for a signed int it is 2147483647. You need to choose a default value of 4294967295 or less. Technically anything you pass greater than 2147483647 will overflow but it happens in such a way as no bits are lost and so the cast back to unsigned at the end brings it back again.

    A nice C++ cross platform alternative to now rather old API calls you are using is the Boost Property Tree.
    W o t . S i g

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    652

    Re: Error Int

    The default value you are passing to GetPrivateProfileInt, 20000000000, is too large to represent with a variable of type INT. An overflow occurs where the actual value passed becomes -1474836480 (2820130816 unsigned) which is probably not what you expect to happen.
    The maximum value that an unsigned int can represent is 4294967295 and for a signed int it is 2147483647. You need to choose a default value of 4294967295 or less. Technically anything you pass greater than 2147483647 will overflow but it happens in such a way as no bits are lost and so the cast back to unsigned at the end brings it back again.
    but as I asked in my post #2, is this being compiled for 32 bit or 64 bit?
    Last edited by 2kaud; Jan 4th, 2015 at 04:27 PM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Error Int

    Hi 2kaud,
    I'm not sure I understand you. If you are suggesting that an int is 64 bit when building for x64 then I don't agree.
    W o t . S i g

  6. #6
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    652

    Re: Error Int

    The size of an int depends upon the compiler as the standard allows this to be implementation specific - though MS VS has an int as 32 bit whether compiling for 32 bit or 64 bit.

    However as GetPrivateProfileInt() has nDefault as type INT (Windows type) which is 32 bit, then as you correctly say 20000000000 is too large for a 32 bit type and is being taken as type __int64 which is then truncated to type INT - hence the warning.

    If numbers greater than can be correctly represented in 32 bits are needed then a new version of GetPrivateProfileInt() would need to be coded - and different data types used. This is what I was trying to come to (unsuccessfully ) with my question re 32 or 64 bit.

    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2019 (16.5.2)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width