Flexible array member

this "error" was discovered on the Arduino IDE 2.0.0 rc6

//FOO.H
#ifndef FOO_H
#define FOO_H
class foo
{
    private :
        uint8_t array [] ;
} ;
#endif

Why does the above code throw the following error :

error: flexible array member 'foo::array' in an otherwise empty 'class foo'


But adding one line dosen't?

//FOO.H
#ifndef FOO_H
#define FOO_H
class foo
{
    private :
        uint8_t intiger ; 
        uint8_t array [] ; //seem flexible to me
} ;
#endif

NOTE THE ADDED uint8_t intiger ; line

Compilation complete.

As far as I know flexible array members aren't a thing in C++

See Are flexible array members valid in C++? - Stack Overflow

See Are flexible array members valid in C++? - Stack Overflow

the code will compile, and work quite well, but it is technically undefined behavior. You can allocate the appropriate memory with an expression that is unlikely to have off-by-one errors:

so even though the latter code is not allowed and/or is undefined behavior my program will still work as I intend it to do?
Or will it cause some memory leak down the road?

If I read the SO thread correctly, your lower example violates the C++ standard but should work fine with gcc/g++ as a compiler anyway. So unless you're worried about using another compiler in the future, you'll probably be fine.

This explains why the second example compiles but the first one does not:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.