This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Advisor: suggesting const static for classes

My code has a number of classes.  These are all instantiated in main.cpp.

Example:

SystemMaster oMaster;
GP22 oGP22;
CurrentData oCurrentData;
OCTControl oOCTControl;
SystemError oError;
LCDIconControl oIconControl;
DataStream oDataStream;
MemSegment oMemSegment;
SpoolPiece oSpoolPiece;
MBUS oMBUS;
MBUSStream    oMBUSStream;

In other source files where a class is needed, I do this at the top of the file:

extern SystemMaster oMaster;


The ULP advisor complains.  It says:


(ULP 8.1) variable "oMaster" is used as a constant. Recommend declaring variable as either 'static const' or 'const'   

Why?

Should this advice be ignored or is there some treatment of cpp classes in CCS that I am missing?

Note: there is currently one instance of each class, but I prefer class notation over structures.

  • Hi Mark,

    ULP8.1 remark is issued where a structure (or an instance of a class) is declared & used as a variable in a function (local variable) but none of its components or variables is modified throughout the entire function. Instantiating large variables like structures or classes can potentially cost several clock cycles at the beginning of the function each time you it is called, so it would be more power-efficient to use static constants instead of variables. 

    You should only get this remark when the above conditions are met, so if you think your classes are either modified (either directly in the function or as a parameter to another function) or declared as global variables instead of local ones, we can help take a closer look at what is going on. If this is the case, could you somehow condense your code to something very simple & generic (to protect your code/IP) that can still reproduce the remark? 

    By the way, here's some more info on the rule, which should be link-able directly from the remark in the compile output window in CCS or IAR: 

    http://processors.wiki.ti.com/index.php/Compiler/diagnostic_messages/MSP430/1535

    Regards,

    Dung

  • Dung,

    A most helpful reply!

    Maybe posting something would be instructive, but first I have at least a clue in your, "used as a variable in a function (local variable)"


    I have many functions which need a temporary unsigned long array (10 elements at most), so in the system master class I define such a variable and simply use it in non-atomic operations.  I considered that declaring the array in each function was wasteful.  Maybe the technique I use is even more wasteful?  Perhaps a global is best in this case?






  • Actually, I do have a bit more information about this issue.

    In main.cpp I have a few classes that are instantiated but not used in main.cpp.

    In sources where these classes are used, they are defined at the top of the source file as extern.

    main.cpp:

       CurrentData  oCurrentData;

    SomeFile.cpp

      extern CurrentData  oCurrentData;

      oCurrentData.Reset();

    The warning is thrown in main.cpp.  As I don't use the object in main.cpp, but have to instantiate the class for global access, I am therefore in a conundrum.

    /m

  • Mark Richards said:
    In main.cpp I have a few classes that are instantiated but not used in main.cpp.

    If there is only one global instance of the class, you may create them as static classes: static member variables and static functions. this way you don't need to dynamically instantiate the classes and have access to their funcitons and members by the class name.
    Another advantage is that the space of the class variables, as they are static, is taken into account at linktime instead of dynamically taken from the heap at runtime.

    Of course htis only works if you don't need to instantiate multiple instances at runtime.

  • Jens-Michael,

    For the class definition below, would I simply add static before each class variable definition?  What about the class methods.. same?

    class CurrentData
    {
        public:


            CurrentData(void);


            float nInTemp;
            float nOutTemp;
            float nTempDiff;
            float nInResistance;
            float nOutResistance;
            float nChipTemperature;
            float psdrift;
            unsigned long R0_Calibration;
            unsigned long R0_PW1ST;
            unsigned long Flow_0; // result registers for flows
            unsigned long Flow_1;
            unsigned long Flow_2;
            unsigned long Flow_3;

            unsigned long Flowreg_down;
            unsigned long Flowreg_up;

            float nominal_flow;

            unsigned int ChipTemperatureSampleTime;
            unsigned long nSampleTime;
            unsigned long nDisplayUpdateTime;

    };

  • Mark Richards said:
    For the class definition below, would I simply add static before each class variable definition?  What about the class methods.. same?

    Almost.

    In the class definition, all member variables and all method declarations require the static keyword.
    I also suggest making the constructor private, so the class cannot be instantiated.

    The method definitions, however, don't have the static keyword on it.

    Now you can turn the whole thing in to just a bunch of global non-member functions. Without a class at all. Unless you want different classes with the same method names but different behaviour.
    The only differences are:
    - you call the functions without the class name prefix
    - private static variables turn into static variables (inaccessible from outside the same cpp file)
    - public static variables turn into non-static global variables. Attention: they will merge with other global variables with the same name (unless you use a namespace). To throw a linker warning if this happens, be sure to initialize your global variables in the cpp file.
    - code is likely to be smaller.
    - in the header file, you only export the function names and any globally visible variables (with extern keyword).

    Static classes are usually a hack of java-like languages that do not allow classless global functions or global variables at all.

**Attention** This is a public forum