Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
becki:linux:cpp [2005-09-29 19:14] 127.0.0.1 external edit |
becki:linux:cpp [2009-06-15 14:35] (aktuell) becki |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | FIXME Hier kommt der c++ Inhalt rein | + | ====== C++ == |
+ | ===== Guidlines for efficient C/C++ Code == | ||
+ | |||
+ | - Pass and return primitives //as value// to and from functions [Breymann] p177 ,[Davis] | ||
+ | - Pass objects as const reference to functions and omit ''const'' only when the function is designed to modify the object [Breymann] p177 | ||
+ | - 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 | ||
+ | - Prefer references to pointers. References (and reference types) are just syntactic sugar for pointers and do not cause runtime overhead [Breymann?], [Quiroz] | ||
+ | - 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. [?] | ||
+ | - Always declare constant primitives and objects with ''const'' | ||
+ | - ''int'' is the most efficient type for local variables. (No filling with zero's necessary) [Davis] | ||
+ | - Use ''stdint.h''. It offers... [Davis] | ||
+ | * exact length types | ||
+ | * smallest types of at least given length and | ||
+ | * most efficient types | ||
+ | - Larger data types as ''int'' are less efficient [Davis] | ||
+ | - Use the ''size_t'' and ''ptdiff_t'' types for memory calculations, not the ''long'' type [Davis] | ||
+ | - Unsigned types tend to be more efficient then signed [Davis] | ||
+ | - 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] | ||
+ | - Declare global data and functions as ''static'' whenever possible but avoid ''static'' variables at function level whenever possible [Davis] | ||
+ | - :?: Use the ''restrict'' keyword for data that is modified by one certain pointer [Davis] | ||
+ | - Declare variables in the innermost scope possible [Davis] | ||
+ | - Prefer float types to double (Use ''3.0f'' instead of ''3.0'' and ''sinf()'' instead of ''sin()'') [Davis] | ||
+ | - It most often more efficient to use the stack rather than the heap for variable length arrays. FIXME Expamle! [Davis] | ||
+ | - Declare a function as inline when it is... [Davis] | ||
+ | * small | ||
+ | * called very often from few places (e.g. inside a loop) | ||
+ | - Use a ''mutex'' for concurrent data access [Obiltschnig] | ||
+ | - Do not use exceptions in realtime / embedded systems. They may have non-deterministic runtime behauviour. Use [[becki/coding/c#error_reporting_in_functions|goto]] as alternative. [Obiltschnig], [Quiroz] | ||
+ | - Use consts rather than preprocessor defines [Quiroz] | ||
+ | - To avoid name collisions use namespaces rather than prefixes or classes [Quiroz] | ||
+ | - 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 | |