Is there any reason to use C instead of C++ for embedded development?

后端 未结 30 3552
既然无缘
既然无缘 2020-11-30 17:20

Question

I have two compilers on my hardware C++ and C89

I\'m thinking about using C++ with classes but without polymorphism (to avoid vtables). The main r

相关标签:
30条回答
  • 2020-11-30 17:52

    My personal preference is C because :

    • I know what every line of code is doing (and costs)
    • I don't know C++ well enough to know what every line of code is doing (and costs)

    Why do people say this? You don't know what every line of C is doing unless you check the asm output. Same goes for C++.

    For example, what asm does this innocent statement produce:

    a[i] = b[j] * c[k];
    

    It looks fairly innocent, but a gcc based compiler produces this asm for an 8-bit micro

    CLRF 0x1f, ACCESS
    RLCF 0xfdb, W, ACCESS
    ANDLW 0xfe
    RLCF 0x1f, F, ACCESS
    MOVWF 0x1e, ACCESS
    MOVLW 0xf9
    MOVF 0xfdb, W, ACCESS
    ADDWF 0x1e, W, ACCESS
    MOVWF 0xfe9, ACCESS
    MOVLW 0xfa
    MOVF 0xfdb, W, ACCESS
    ADDWFC 0x1f, W, ACCESS
    MOVWF 0xfea, ACCESS
    MOVFF 0xfee, 0x1c
    NOP
    MOVFF 0xfef, 0x1d
    NOP
    MOVLW 0x1
    CLRF 0x1b, ACCESS
    RLCF 0xfdb, W, ACCESS
    ANDLW 0xfe
    RLCF 0x1b, F, ACCESS
    MOVWF 0x1a, ACCESS
    MOVLW 0xfb
    MOVF 0xfdb, W, ACCESS
    ADDWF 0x1a, W, ACCESS
    MOVWF 0xfe9, ACCESS
    MOVLW 0xfc
    MOVF 0xfdb, W, ACCESS
    ADDWFC 0x1b, W, ACCESS
    MOVWF 0xfea, ACCESS
    MOVFF 0xfee, 0x18
    NOP
    MOVFF 0xfef, 0x19
    NOP
    MOVFF 0x18, 0x8
    NOP
    MOVFF 0x19, 0x9
    NOP
    MOVFF 0x1c, 0xd
    NOP
    MOVFF 0x1d, 0xe
    NOP
    CALL 0x2142, 0
    NOP
    MOVFF 0x6, 0x16
    NOP
    MOVFF 0x7, 0x17
    NOP
    CLRF 0x15, ACCESS
    RLCF 0xfdf, W, ACCESS
    ANDLW 0xfe
    RLCF 0x15, F, ACCESS
    MOVWF 0x14, ACCESS
    MOVLW 0xfd
    MOVF 0xfdb, W, ACCESS
    ADDWF 0x14, W, ACCESS
    MOVWF 0xfe9, ACCESS
    MOVLW 0xfe
    MOVF 0xfdb, W, ACCESS
    ADDWFC 0x15, W, ACCESS
    MOVWF 0xfea, ACCESS
    MOVFF 0x16, 0xfee
    NOP
    MOVFF 0x17, 0xfed
    NOP
    

    The number of instructions produced depends massively on:

    • The sizes of a, b, and c.
    • whether those pointers are stored on the stack or are global
    • whether i, j and k are on the stack or are global

    This is especially true in the tiny embedded world, where processors are just not set up to handle C. So my answer would be that C and C++ are just as bad as each other, unless you always examine the asm output, in which case they are just as good as each other.

    Hugo

    0 讨论(0)
  • 2020-11-30 17:53

    Personally with 4kb of memory I'd say you are not getting that much more mileage out of C++, so just pick the one that seems the best compiler/runtime combination for the job, since language is probably not going to matter much.

    Note that it is also not all about language anyway, since also the library matters. Often C libs have a slightly smaller minimum size, but I could imagine that a C++ lib targeted at embedded development is cut down, so be sure to test.

    0 讨论(0)
  • 2020-11-30 17:54

    How much ROM/FLASH do you have?

    4kB of RAM can still mean there are hundreds of kilobytes of FLASH to store the actual code and static data. RAM on this size tends to be meant just for variables, and if you are careful with those you can fit quite a large program in terms of code lines into memory.

    However, C++ tends to make putting code and data in FLASH more difficult, due to the run-time construction rules for objects. In C, a constant struct can easily be put into FLASH memory and accessed as a hardware-constant object. In C++, a constant object would require the compiler to evaluate the constructor at compile-time, which I think is still beyond what a C++ compiler can do (theoretically, you could do it, but it is very very hard to do in practice).

    So in a "small RAM", "large FLASH" kind of environment I would go with C any day. Note that a good intermediate choice i C99 which has most of the nice C++ features for non-class-based-code.

    0 讨论(0)
  • 2020-11-30 17:54

    You have inline in C99. Maybe you like ctors, but the business of getting dtors right can be messy. If the remaining only reason to not use C is namespaces, I would really stick to C89. This is because you might want to port it to a slightly different embedded platform. You may later start writing in C++ on that same code. But beware the following, where C++ is NOT a superset of C. I know you said you have a C89 compiler, but does this C++ comparison with C99 anyway, as the first item for example is true for any C since K&R.

    sizeof 'a' > 1 in C, not in C++. In C you have VLA variable length arrays. Example: func(int i){int a[i]. In C you have VAM variable array members. Example: struct{int b;int m[];}.

    0 讨论(0)
  • 2020-11-30 17:55

    I see no reason to use C instead of C++. Whatever you can do in C, you can do it also in C++. If you want to avoid overheads of VMT, don't use virtual methods and polymorphism.

    However, C++ can provide some very useful idioms with no overhead. One of my favourites is RAII. Classes are not necessary expensive in terms of memory or performance...

    0 讨论(0)
  • 2020-11-30 17:57

    I've written some code for ARM7 embedded paltform on IAR Workbench. I highly recommend relying on templates to do compile-time optimization and path prediction. Avoid dynamic casting like plague. Use traits/policies to your advantage, as prescribed in Andrei Alexandrescu's book, Modern C++ design.

    I know, it can be hard to learn, but I am also sure that your product will benefit from this approach.

    0 讨论(0)
提交回复
热议问题