Home AMX User Forum NetLinx Studio

Should I live with compiler warnings about CONSTANT declarations?

I'm a relatively new Netlinx programmer, and keep getting warning messages when I try to compile code that has some CONSTANTs defined. It offends my sense of neatness, but it compiles OK. Should I just live with the warning messages? Is there a better way of defining these constants? Sample code was created and pasted into a new Netlinx template was:
(***********************************************************)
DEFINE_CONSTANT
CONSTANT HIGHSLONG = 2147483647
CONSTANT LOWSLONG = -2147483647
CONSTANT LOWSLONG_00 = -1
CONSTANT LOWSLONG_01 = -10
CONSTANT LOWSLONG_02 = -100
CONSTANT LOWSLONG_03 = -1000
CONSTANT LOWSLONG_04 = -10000
CONSTANT LOWSLONG_05 = -100000
CONSTANT LOWSLONG_06 = -1000000
CONSTANT LOWSLONG_07 = -10000000
CONSTANT LOWSLONG_08 = -100000000
CONSTANT LOWSLONG_09 = -1000000000
CONSTANT LOWSLONG_10 = -2147483640
CONSTANT SLONG HIGHSLONGA = 2147483647
CONSTANT SLONG LOWSLONGA = -2147483647
CONSTANT SLONG LOWSLONG_00A = -1
CONSTANT SLONG LOWSLONG_01A = -10
CONSTANT SLONG LOWSLONG_02A = -100
CONSTANT SLONG LOWSLONG_03A = -1000
CONSTANT SLONG LOWSLONG_04A = -10000
CONSTANT SLONG LOWSLONG_05A = -100000
CONSTANT SLONG LOWSLONG_06A = -1000000
CONSTANT SLONG LOWSLONG_07A = -10000000
CONSTANT SLONG LOWSLONG_08A = -100000000
CONSTANT SLONG LOWSLONG_09A = -1000000000
CONSTANT SLONG LOWSLONG_10A = -2147483640
CONSTANT PLACEHOLDER = 1
(***********************************************************)
Compiling it gives the following warnings:

Starting NetLinx Compile - Version[2.5.2.20] [01-06-2014 12:00:30]
C:\AMX-work\TestOfConstantsDeclarations.axs
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(29): C10571: Converting type [INTEGER] to [INTEGER]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(30): C10571: Converting type [INTEGER] to [INTEGER]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(38): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(44): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(45): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(46): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(47): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(48): C10571: Converting type [LONG] to [SLONG]
WARNING: C:\AMX-work\TestOfConstantsDeclarations.axs(49): C10571: Converting type [LONG] to [SLONG]
C:\AMX-work\TestOfConstantsDeclarations.axs - 0 error(s), 9 warning(s)

The warning messages seem inconsistent in where they say the error is, as there are supposed to be nine problems, but there are more than this number of CONSTANT declarations that are essentially identical.

Should I just ignore this type of message as a peculiarity of the Netlinx compiler?

Bill Lee

Comments

  • wengerpwengerp Posts: 29
    Bill Lee wrote: »
    Should I just ignore this type of message as a peculiarity of the Netlinx compiler?

    No, you should not ;-) I personally never release any code with compiler warnings (although they sometimes are hard to understand or - more often - are missing).

    I'm not sure about your problem but have you tried to compile it without the "CONSTANT" key word? Since your constant declarations are within the DEFINE_CONSTANT section it is not necessary to use the CONSTANT key word (this is meant to be used in the DEFINE_VARIABLE section). Also be aware that if you omit the type of a variable or constant declaration the compiler assumes the type integer for number values and char for others. So your first few statements are actually all integer declarations but since you initialize (some of) them with a negative value the compiler has to cast them to a slong value (hence the warning). But this doesn't explain the fact that there are only nine warnings.

    Patrick
  • rynandorynando Posts: 68
    You can disable that compiler warning by including the following compiler directive:

    #DISABLE_WARNING 10571
  • the8thstthe8thst Posts: 470
    Wrapping the negative numbers in double quotes fixes the warnings:
    DEFINE_CONSTANT
    CONSTANT HIGHSLONG = 2147483647
    CONSTANT LOWSLONG = "-2147483647"
    CONSTANT LOWSLONG_00 = "-1"
    CONSTANT LOWSLONG_01 = "-10"
    CONSTANT LOWSLONG_02 = "-100"
    CONSTANT LOWSLONG_03 = "-1000"
    CONSTANT LOWSLONG_04 = "-10000"
    CONSTANT LOWSLONG_05 = "-100000"
    CONSTANT LOWSLONG_06 = "-1000000"
    CONSTANT LOWSLONG_07 = "-10000000"
    CONSTANT LOWSLONG_08 = "-100000000"
    CONSTANT LOWSLONG_09 = "-1000000000"
    CONSTANT LOWSLONG_10 = "-2147483640"
    CONSTANT SLONG HIGHSLONGA = 2147483647
    CONSTANT SLONG LOWSLONGA = "-2147483647"
    CONSTANT SLONG LOWSLONG_00A = "-1"
    CONSTANT SLONG LOWSLONG_01A = "-10"
    CONSTANT SLONG LOWSLONG_02A = "-100"
    CONSTANT SLONG LOWSLONG_03A = "-1000"
    CONSTANT SLONG LOWSLONG_04A = "-10000"
    CONSTANT SLONG LOWSLONG_05A = "-100000"
    CONSTANT SLONG LOWSLONG_06A = "-1000000"
    CONSTANT SLONG LOWSLONG_07A = "-10000000"
    CONSTANT SLONG LOWSLONG_08A = "-100000000"
    CONSTANT SLONG LOWSLONG_09A = "-1000000000"
    CONSTANT SLONG LOWSLONG_10A = "-2147483640"
    CONSTANT PLACEHOLDER = 1
    

    I'm sure I will think of why that solves the problem at some point, but it doesn't make sense to me right now either.
  • PhreaKPhreaK Posts: 966
    By wrapping those negative numbers in quotes you are forcing the compiler to see them as a character (aka byte) value. If you were to open up your debugger an inspect say LOWSLONG you will notice that the value in there probably is not what you are expecting. From the NetLinx help file:
    If type conversion is required for an assignment or as a result of a parameter or return type mismatch, the value is converted to fit the type of the target variable. This may involve truncating the high order bytes(s) when converting to a smaller size variable or sign conversion when converting signed values to unsigned or vice versa.

    Now, when it comes to the constant keyword. This is a modifier that can be used when defining global variables. It basically allows you to mark variables as immutable and keep all of your definitions in the define_variable section, thus removing the need for a define_constant section. The approach to use is your choice, however placing a constant modifier within a define_constant section serves no purpose.

    What you do need to do however is specify a type if you want anything other than integer or character values (which, looking at your values, you do). The format is this:
    [NON_VOLATILE | VOLATILE | PERSISTENT] [CONSTANT] [<type>] name [= <value>]
    

    So taking your LOWSLONG again as an example it could be defined as either
    define_constant
    slong LOWSLONG = -2147483647
    
    or
    define_variable
    constant slong LOWSLONG = -2147483647
    

    P.S. disabling those compiler warning is like taking the battery out of your smoke alarm as your house is burning down because the beeping is stopping you from sleeping.
  • ericmedleyericmedley Posts: 4,177
    There are a few occasions where you can get code that "works as expected" but is throwing a warning. This is usually a symptom of goofy type casting or in other words, it works this way in another development language but not really in Netlinx. I usually find these type cast problems and resolve the warning. I echo Phreak's sentiments with a slightly different illustration. Turning off warnings is like putting tape over the warning lights on your car dashboard. The car might still run but there is clearly a problem that might burn you later.
  • Netlinx compiler warns on constants smaller than -65535 allocated to SLONGs

    Thanks to the people who replied to my original posting.

    OK, in theory I agree: Ignoring compiler warnings are like ignoring the warning lights on the dashboard when you drive around. There are some occasions when you do have to ignore the warning lights though - but the important thing to know is when to ignore the lights because they are mistakenly indicating a fault in the engine but the fault is really in the monitoring circuitry.

    In the meantime I've been off programming, writing a program that keeps track of a club membership for 600 members. This code allows RFID swipe or passcode manual validation, records attendance, allows entry of competition scores and can print receipts to a thermal printer. I wouldn't say it was perfect code, but it certainly allowed me to practice writing, building and uploading code.

    Just now I did some more testing of the compiler and found the following results:

    (***********************************************************)
    DEFINE_CONSTANT
    SLONG ASLONG = 2147483648 // !!! too big for a SLONG
    SLONG BSLONG = 2147483647 // largest possible SLONG
    SLONG CSLONG = 2147483646 // OK
    SLONG DSLONG = 1 // OK
    SLONG ESLONG = 0 // OK
    SLONG FSLONG = -1 // OK
    SLONG GSLONG = -32767 // OK
    SLONG HSLONG = -32768 // OK
    SLONG ISLONG = -65535 // OK
    SLONG JSLONG = -65536 // !!!
    SLONG KSLONG = -2147483647 // !!! smallest possible SLONG
    (***********************************************************)

    When the compiler was run the lines above marked with !!! had the following warning message
    WARNING: C:\AMX-work\TestProg.axs(<line number + 1>): C10571: Converting type [LONG] to [SLONG]

    Of these warning messages, only the warning for "SLONG ASLONG = 2147483648" is truly a legitimate warning of an improper allocation.

    OK, now what happens if you try to do things with variables:
    (***********************************************************)
    DEFINE_VARIABLE
    SLONG fred
    (***********************************************************)
    DEFINE_START
    fred = -65534 // OK
    fred = -65535 // OK
    fred = -65536 // !!!
    (***********************************************************)

    The same warning message "Converting type [LONG] to [SLONG]" is given for the allocation of a number that should be OK for allocation to a SLONG variable.

    Based on the help from the posters to my original question, I've made the changes in my definitions of Constants.

    My conclusions are that the Netlinx compiler Version[2.5.2.20] will throw a warning message if you try to allocate a constant number to a SLONG variable if that constant number is lower than -65535. This is almost certainly just one of the quirks of the compiler since it should otherwise be OK to do, as long as this negative number is > -2147483647. I can't see a nice way of working around this and if you need to do this then you would probably have to just live with this warning message in this case.

    The reason I wanted a very small SLONG number was to use this as an indicator that a returned number was outside acceptable limits. Of course I think now that maybe I should use an extremely negative but recognisable and testable number (like -1000000) instead of a number ( -(2^32 - 1)) that might might be generated inside the program if a calculation rolls over 32 bits.

    Thanks,
    Bill Lee
  • Constant warnings

    It is unnecessary to declare the type of constants you are defining.
    If you will define the constant without the SLONG type declaration, the compiler will automatically type the value appropriately. The following compiles without any warning.
    DEFINE_CONSTANT
    MYSLONG = -2147483647 // or whatever

    I know that many programmers have seen these warnings as the Forum thread shows. But the developers of the compiler simply did not expect programmers to try to assign types to the constants. So, when the additional information is included, the compiler is throwing the warning because of that unexpected data. Since constants cannot change, there is no need for that declaration.
  • The latest version of the compiler (2.5.2.300) does this as well.

    I think it's important to point out that, as Bob says, types for constants aren't expected and (except in this case) the compiler seems to completely ignore them and use them however seems most appropriate. For example:
    DEFINE_CONSTANT
       INTEGER C_NUMBER = 3;
    
    DEFINE_VARIABLE
       INTEGER V_NUMBER = 3;
       FLOAT F_NUMBER = 3;
    
    DEFINE_START
       SEND_STRING 0, "'C_NUMBER : ', FTOA(2/C_NUMBER)"; // Prints 0.666667
       SEND_STRING 0, "'V_NUMBER : ', FTOA(2/V_NUMBER)"; // Prints 0
       SEND_STRING 0, "'F_NUMBER : ', FTOA(2/F_NUMBER)"; // Prints 0.666667
    

    Finally, after reading about this issue I decided to try out some more ways to assign SLONGs and found that there is in fact a bug in the compiler. See the attached code if you'd like to try it out yourself. I'll also paste it below:

    PROGRAM_NAME='SignedLongAssignmentTest'
    // NOTE: The compiler's warnings point to __LINE__ + 1
    
    DEFINE_CONSTANT
       C_NO_TYPE                     = -65536;                  // OK
       SLONG C_TYPE                  = -65536;                  // WARNING: C10571: (7): Converting type [LONG] to [SLONG]
       C_NO_TYPE_AFTER_TYPE          = -65536;                  // WARNING: C10571: (8): Converting type [LONG] to [SLONG]
    
    DEFINE_VARIABLE
       SLONG slVarEmpty;                                        // OK
       SLONG slVarHardInit           = -65536;                  // WARNING: C10571: (12): Converting type [LONG] to [SLONG]
       SLONG slVarConstNoTypeInit    = C_NO_TYPE;               // WARNING: C10571: (13): Converting type [LONG] to [SLONG]
       SLONG slVarConstTypeInit      = C_TYPE;                  // WARNING: C10571: (7): Converting type [LONG] to [SLONG] // (Points to C_TYPE line)
       SLONG slVarConstAfterTypeInit = C_NO_TYPE_AFTER_TYPE;    // WARNING: C10571: (8): Converting type [LONG] to [SLONG] // (Points to C_NO_TYPE_AFTER_TYPE line)
       CONSTANT C_VAR_NO_TYPE        = -65536;                  // WARNING: C10571: (16): Converting type [LONG] to [INTEGER]
       CONSTANT SLONG C_VAR_TYPE     = -65536;                  // WARNING: C10571: (17): Converting type [LONG] to [SLONG]
    
    DEFINE_START
       slVarEmpty                    = -65536;                  // WARNING: C10571: (20): Converting type [LONG] to [SLONG]
       slVarEmpty                    = C_NO_TYPE;               // WARNING: C10571: (21): Converting type [LONG] to [SLONG]
       slVarEmpty                    = C_TYPE;                  // WARNING: C10571: (22): Converting type [LONG] to [SLONG]
       slVarEmpty                    = C_NO_TYPE_AFTER_TYPE;    // WARNING: C10571: (23): Converting type [LONG] to [SLONG]
       slVarEmpty                    = slVarHardInit;           // OK
       slVarEmpty                    = slVarConstNoTypeInit;    // OK
       slVarEmpty                    = slVarConstTypeInit;      // OK
       slVarEmpty                    = slVarConstAfterTypeInit; // OK
       slVarEmpty                    = C_VAR_NO_TYPE;           // WARNING: C10571: (28): Converting type [LONG] to [SLONG]
       slVarEmpty                    = C_VAR_TYPE;              // WARNING: C10571: (29): Converting type [LONG] to [SLONG]
    
Sign In or Register to comment.