-2

I want to design an agent-based control system for an embedded application with multiple Arduino Uno microcontrollers. I would like to do so in C++, mainly because it's the language I am most familiar with. I know run-time memory allocation and other dynamic operations are a concern (new and throw are seemingly the big baddies), and found some warnings against the usage of virtual base classes and multiple inheritance (I am no fan of the latter in any application). But I have been unable to find any reference to inheritance in general, and especially multilevel inheritance.

As far as I know, inheritance-tree depth should not affect performance, but I just wanted to make sure before kicking off. I'm also interested in advice regarding coding style (i.e., whether it is, in general, advisable to use inheritance on embedded systems).

Thank you for your help. Please let me know if I need to elaborate more.

borizzzzz
  • 620
  • 1
  • 6
  • 17
  • 1
    http://stackoverflow.com/questions/7210412/what-is-the-cost-of-inheritance – Lanting May 11 '17 at 14:01
  • If you talk about EC++ (Embedded C++) multiple inheritance probably won't work. You can use composition though. – mutantkeyboard May 11 '17 at 14:02
  • Thanks for the comments. @mutantkeyboard, why wouldn't multiple inheritance work in EC++? As far as I understand, it's only a dialect of C++... – borizzzzz May 11 '17 at 14:48
  • I don't know if they changed anything in the recent implementations, but if I recall it was removed from Embedded C++ – mutantkeyboard May 11 '17 at 14:55
  • 2
    @mutantkeyboard: EmbeddedC++ is dead since a long time. It never really made it to a broader audience. Arduino is not exactly C++. – too honest for this site May 11 '17 at 23:50
  • It is actually irrelevant if it is _possible_, because it is a bad idea from the beginning to bloat your framework unnecessarily on such a limited platform. – too honest for this site May 11 '17 at 23:51
  • Thanks for your comments @Olaf. If inheritance has no cost, why would using it bloat the system? The very reason of using inheritance in this case would be to reuse as much code as possible. What would, in your opinion, be a better way of creating agents (on such platforms)? – borizzzzz May 12 '17 at 12:40
  • Where did I say _inheritance_ per se has a cost?? "The very reason of using inheritance in this case would be to reuse as much code as possible" - 1) This has nothing to do with inheritance. 2) If you use virtual methods, what do you think happens with the methods of the base-class? They don't magically disappear from your final code unless the toolchain can **prove** they are never used - problematic at best. – too honest for this site May 12 '17 at 17:10
  • 2
    Static inheritance(i.e. no virtual functions), no matter how many levels deep, has no inherent cost. The compiler knows everything and calling MyParentClass::Function() from MyGrandchild::Function() is no different than calling MyParentClass_Function(). In both cases, you load up an address and jump to it(or maybe it can be encoded into the instruction) . Virtual functions has a very slight overhead(one more indirection), but as long as it's not some very tight loop, then it's not much to worry about. – Russ Schultz May 12 '17 at 18:16

1 Answers1

1

Inheritance doesn't matter, except for the few caveats already mentioned in comments. The constraints you face are those of the microcontroller at the heart of the Uno. It has 2048 bytes of RAM, 1024 bytes of EEPROM, and 32K of flash. Study the datasheet of the ATmega 328 and if your program will fit and run, your inheritance hierarchy won't matter.

TomServo
  • 7,248
  • 5
  • 30
  • 47