Difference between revisions of "C/C++"
From HCL
(→Coding) |
(→General) |
||
Line 21: | Line 21: | ||
* [http://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html Handling program arguments] (avoid <code>argp</code> since it is not supported on many platforms) | * [http://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html Handling program arguments] (avoid <code>argp</code> since it is not supported on many platforms) | ||
* [http://en.wikipedia.org/wiki/Dynamic_loading Dynamic loading of shared libraries] | * [http://en.wikipedia.org/wiki/Dynamic_loading Dynamic loading of shared libraries] | ||
+ | * Avoid [http://en.wikipedia.org/wiki/Variable-length_array variable-length arrays]. First, GCC allocates them on the stack. Second, the status of this feature in GCC is BROKEN. Third, never do this: | ||
+ | <source lang="C"> | ||
+ | int size; | ||
+ | MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
+ | char names[size][MPI_MAX_PROCESSOR_NAME]; | ||
+ | </source> |
Revision as of 13:47, 18 November 2010
Contents
Coding
- C++ programming style is preferrable. For example, in variable declarations, pointers and references should have their reference symbol next to the type rather than to the name. Variables should be initialized where they are declared, and should be declared where they are used. For more details, see Google C++ Style Guide
- One-true-brace ident style
- Coding header files
- Learn from examples and use coding approaches from third-party software
Commenting
- Place Doxygen comments in header files (before declarations of namespaces/classes/structs/typedefs/macros) and main source files (for documenting tools and tests)
- Use double forward slash for short comments in the code
C++
- Mixing C/C++
- Provide main API in C
- Use plain C unless you need flexible data structures or STL/Boost functionality
- Template C++ is preferrable from the point of view of runtime performance
- Mind the life cycle of objects: Default constructor Copy constructor, Destructor
- Force C++ linking
General
- Don't use non-standard functions, like itoa
- Handling program arguments (avoid
argp
since it is not supported on many platforms) - Dynamic loading of shared libraries
- Avoid variable-length arrays. First, GCC allocates them on the stack. Second, the status of this feature in GCC is BROKEN. Third, never do this:
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
char names[size][MPI_MAX_PROCESSOR_NAME];