Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:linux:cpp

C++

Guidlines for efficient C/C++ Code

  1. Pass and return primitives as value to and from functions [Breymann] p177 ,[Davis]
  2. Pass objects as const reference to functions and omit const only when the function is designed to modify the object [Breymann] p177
  3. Return objects which are attributes of the class as const reference and objects wich are not class attributes (especially stack and heap :?: objects) as value! [Breymann] p177
  4. Prefer references to pointers. References (and reference types) are just syntactic sugar for pointers and do not cause runtime overhead [Breymann?], [Quiroz]
  5. To avoid memory leaks encapsulate new and delete in the constructor and destructor of a class. Than create the instance of the class as automatic variable. [?]
  6. Always declare constant primitives and objects with const
  7. int is the most efficient type for local variables. (No filling with zero's necessary) [Davis]
  8. Use stdint.h. It offers… [Davis]
    • exact length types
    • smallest types of at least given length and
    • most efficient types
  9. Larger data types as int are less efficient [Davis]
  10. Use the size_t and ptdiff_t types for memory calculations, not the long type [Davis]
  11. Unsigned types tend to be more efficient then signed [Davis]
  12. Plain char is always the most efficient char type. For signed or unsigned char it depends on the target which one is more efficient. [Davis]
  13. Declare global data and functions as static whenever possible but avoid static variables at function level whenever possible [Davis]
  14. :?: Use the restrict keyword for data that is modified by one certain pointer [Davis]
  15. Declare variables in the innermost scope possible [Davis]
  16. Prefer float types to double (Use 3.0f instead of 3.0 and sinf() instead of sin()) [Davis]
  17. It most often more efficient to use the stack rather than the heap for variable length arrays. FIXME Expamle! [Davis]
  18. Declare a function as inline when it is… [Davis]
    • small
    • called very often from few places (e.g. inside a loop)
  19. Use a mutex for concurrent data access [Obiltschnig]
  20. Do not use exceptions in realtime / embedded systems. They may have non-deterministic runtime behauviour. Use goto as alternative. [Obiltschnig], [Quiroz]
  21. Use consts rather than preprocessor defines [Quiroz]
  22. To avoid name collisions use namespaces rather than prefixes or classes [Quiroz]
  23. Aviod Run-Time Type Identification (RTTI) at least for realtime / embedded systems [Quiroz]

References

[Breymann] Ulrich Breymann. C++ Einführung und professionelle Programmierung
[Davis] Greg Davis. Guidlines for efficient C/C++ Code. Lecture at the „Embedded Systems“ fair in Nuremberg 2009-03-04
[Obiltschnig] Günter Obiltschnig. C++ for Safety-critical Systems. Lecture at the „Embedded Systems“ fair in Nuremberg 2009-03-04
[Quiroz] César A Quiroz. Using C++ Efficiently In Embedded Applications. Mentor Graphics, Embedded Software Division
Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
becki/linux/cpp.txt · Zuletzt geändert: 2009-06-15 14:35 von becki

Impressum - Datenschutzerklärung