Search code examples
c++microcontroller

Initialize class with array


I found many questions in this area but none seem to match what I'm looking for.

The goal is to initialize a constant class instance so that it is put completely in flash memory of an microcontroller and not take up any ram.

This is an not working example:

   class Message {

            public:

                    const int _addr;
                    const char _data[ 4 ];

                    Message( int addr, const char data[4] )
                                    : _addr( addr )
                                    , _data( data )
                                    {}
    };

    const Message m[] = { 
            Message( 0x123, { 4, 5, 6, 7 } ),
            Message( 0x234, { 5, 6, 7, 8 } )
    };

Using g++11 this fails with the Message:

    g++     test.cpp   -o test
    test.cpp: In constructor ‘Message::Message(int, const char*)’:
    test.cpp:10:20: error: incompatible types in assignment of ‘const char*’ to ‘const char [4]’

Using different combinations of bracket it fails with other errors...

Having pointers to other parts of flash will work and put everything in flash but is not what I'm looking for. (And it needs extra space)

If I would do it in C I would do it this way:

typedef struct {                                                                                                                                                         
                                                                                                                                                                         
    const int _addr;                                                                                                                                                     
    const char _data[ 4 ];                                                                                                                                               
                                                                                                                                                                         
} Message;                                                                                                                                                               
                                                                                                                                                                         
const Message m[] = {                                                                                                                                                    
    { 0x123, { 4, 5, 6, 7 } },                                                                                                                                           
    { 0x234, { 5, 6, 7, 8 } }                                                                                                                                            
};                                                                                                                                                                       

Solution

  • As explained here (kudos to taiBsu for posting the linke in the question's comments)

    Your constructor argument [data] is, actually, not an array! Yes, I know it looks like one, because you wrote [char data[4]]. But, actually, it's [char* data].

    ...

    So, the error message is telling you that you cannot assign a pointer to an array. Fair enough. Doesn't really matter anyway, since the language also doesn't let you assign arrays to arrays. Lol.

    However you can do something like this using aggregate initialization:

    struct Message {
        const int _addr;
        const char _data[ 4 ];
    };
    
    const Message m[] = { 
        Message{ 0x123, { 4,5,6,7}},
        Message{ 0x234, { 5,6,7,8}}
    };
    

    live demo