-2

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 } }                                                                                                                                            
};                                                                                                                                                                       
Scheintod
  • 7,953
  • 9
  • 42
  • 61
  • Maybe [this](https://stackoverflow.com/q/18962441/8359552) question is helpful. – StefanKssmr May 07 '21 at 10:21
  • Or maybe [that one](https://stackoverflow.com/questions/33804462/error-incompatible-types-in-assignment-of-char-to-char-20). – tai May 07 '21 at 10:23
  • btw, when you edit the question you need not add notes like "Edit: s/8/4". If someone is interested in the history of the post, they can look at it [here](https://stackoverflow.com/posts/67432798/revisions). Often adding "Edit" to the question makes it harder to comprehend the question rather than making it simpler. https://meta.stackexchange.com/questions/127639/when-is-edit-update-appropriate-in-a-post – 463035818_is_not_an_ai May 07 '21 at 10:30
  • The parameter const char data[4] decays in a const char * so you lose the size (4). [Look at here](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – Jimmy Loyola May 07 '21 at 10:40
  • I just noticed your "if I would do it in C" edit: in c++ its exactly the same. See [here](https://godbolt.org/z/x4ca6ncbn). Typically I prefer the `struct Message {};` syntax over `typedef struct {} Message;`. – StefanKssmr May 08 '21 at 14:16

2 Answers2

2

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

StefanKssmr
  • 1,196
  • 9
  • 16
  • 2
    Thank you. I didn't even know this bracket/name combination was in the language ... seems to answer the question as i posted it. I'm still hoping for something which uses constructors even they are not strictly needed here. I regard this as a little creepy :) – Scheintod May 07 '21 at 10:58
  • @Scheintod so you think you understand constructors, but array notation and the notion of char * is strange to you? Perhaps a little study is in order. – TomServo May 08 '21 at 01:38
0

Something like this could work:

class Message {

            private:

                    const int _addr;
                    const char * _data;
public:
                    Message( int addr, const char * data )
                            : _addr( addr )
                            , _data( data )
                            {}
    };


    char mm1[] = {4, 5, 6, 7};
    char mm2[] = {5, 6, 7, 8};
    const Message m[] = { 
            Message( 0x123, mm1 ),
            Message( 0x234, mm2 )
    };
    
Roy2511
  • 938
  • 1
  • 5
  • 22
  • This solves the problem, but doesn't answer the question on why it fails. – tai May 07 '21 at 10:24
  • There's an answer to the question [here](https://stackoverflow.com/a/33804607/4217759). – tai May 07 '21 at 10:25
  • Thank you for your answer. I know this works. But It doesn't accomplish what I want. This creates a class with a value and a pointer to somewhere else. I want to array to be "in" the class. – Scheintod May 07 '21 at 10:30