This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Can a member variable be declared const in a class without initialization?

Note: This was originally posted on 5th January 2010 at http://forums.arm.com

I am trying to define a class providing an interface to hardware registers. Some of the registers are read-only. I would like to declare these registers const so they cannot be written, but the compiler is insisting I initialize them. But by initializing them, it is guranteed they will be written.

Does anyone have a solution to this dilemma?

Thanks.

Sincerely,
Steve.
  • Note: This was originally posted on 5th January 2010 at http://forums.arm.com

    Thanks for the input.

    The codebase I am working with uses C++ extensively.  It uses the linker to locate the object instantiated from the hardware registers class.

    Here is an example:

    (in registers.h file)
    class c_Registers
    {
    public:
        UINT32 GetRegister3() { return rw_register_3; }
        SetRegister3(UINT32 value) { rw_register_3 = value; }
        UINT32 GetRegister4() { return ro_register_3; }

    private:
        UINT32 rw_register_1;
        UINT32 rw_register_2;
        UINT32 rw_register_3;
        UINT32 const ro_register_4;
        UINT32 const ro_register_5;
    };

    (in registers.cpp file)
    #pragma arm section zidata = "REG"
    class c_Registers Registers;

    (in scatter load file)
    HW_REGISTERS_HDSECT  0xA000000  UNINIT
    {
        registers.o (REG)
    }

    What I want to do is be able to declare read only registers, ro_register_4 and ro_register_5, as const to protect against anyone adding code that would write the registers.

    Unfortunately, the compiler wants me to initialize the const variables.

    Thanks for your help.

    Sincerely,
    Steve.
  • Note: This was originally posted on 6th January 2010 at http://forums.arm.com

    IMHO, using pragmas and modifying scatter file is overkill for this.
    But if you desperately have to do it this way, just initialize the const variables.
    These const variables will be put in place long before you powerup that hardware.

    I dont know your particular hardware but there should be no problem. Even if the
    hardware block is running, writing to a read-only hardware register should not matter.
    That particular hardware should simply ignore that write request, and that is all...


    Thanks for your response.  Wouldn't there be benefit in having the linker know about the register space within the memory map?  Similar to using 'const' to catch unwanted register writes to a read only register at compile time, this would allow the linkage to catch infringements on the memory mapped registers.
  • Note: This was originally posted on 5th January 2010 at http://forums.arm.com

    I am trying to define a class providing an interface to hardware registers. Some of the registers are read-only. I would like to declare these registers const so they cannot be written, but the compiler is insisting I initialize them. But by initializing them, it is guranteed they will be written.

    Does anyone have a solution to this dilemma?

    Thanks.

    Sincerely,
    Steve.


    If this variable (hardware register in fact) is member of the class,
    I wonder how you will set it at its specific address. I follow a similar
    approach , somewhat different. Below is my I2C interface

    typedef volatile struct _S_DX_i2c_ptr{

       Ui32 _CONSET;
    const Ui32 _STAT;
       Ui32 _DAT;
       Ui32 _ADR;
       Ui32 _SCLH;
       Ui32 _SCLL;
       Ui32 _CONCLR;

    }S_DX_i2c_ptr;

    As you see I can declare some variables as const (as it should be).
    Then I just use a pointer (of this struct type) to access the I2C block.
    S_DX_i2c_ptr * I2C_dev = (S_DX_i2c_ptr *)I2C0_BASE_ADDR;
    You may have such a pointer in you class to access hardware
    registers and some will be R/W , some will be const.

    Good Day...
  • Note: This was originally posted on 6th January 2010 at http://forums.arm.com

    Thanks for the input.

    The codebase I am working with uses C++ extensively.  It uses the linker to locate the object instantiated from the hardware registers class.

    Here is an example:

    (in registers.h file)
    class c_Registers
    {
    public:
        UINT32 GetRegister3() { return rw_register_3; }
        SetRegister3(UINT32 value) { rw_register_3 = value; }
        UINT32 GetRegister4() { return ro_register_3; }

    private:
        UINT32 rw_register_1;
        UINT32 rw_register_2;
        UINT32 rw_register_3;
        UINT32 const ro_register_4;
        UINT32 const ro_register_5;
    };

    (in registers.cpp file)
    #pragma arm section zidata = "REG"
    class c_Registers Registers;

    (in scatter load file)
    HW_REGISTERS_HDSECT  0xA000000  UNINIT
    {
        registers.o (REG)
    }

    What I want to do is be able to declare read only registers, ro_register_4 and ro_register_5, as const to protect against anyone adding code that would write the registers.

    Unfortunately, the compiler wants me to initialize the const variables.

    Thanks for your help.

    Sincerely,
    Steve.


    IMHO, using pragmas and modifying scatter file is overkill for this.
    But if you desperately have to do it this way, just initialize the const variables.
    These const variables will be put in place long before you powerup that hardware.

    I dont know your particular hardware but there should be no problem. Even if the
    hardware block is running, writing to a read-only hardware register should not matter.
    That particular hardware should simply ignore that write request, and that is all...
  • Note: This was originally posted on 5th January 2010 at http://forums.arm.com

    Are you intentionally using a C++ compiler; the following is legal C:

    typedef volatile struct device_s {
      int rw;
      const int ro;
    } device_t;

    device_t *dev = (device_t*)0xA0000000;

    void bar(void)
    {
      dev->rw = dev->ro;
      dev->rw = dev->ro;
     
      //  dev->ro = 3; // would generate error
    }


    hth
    s.