Difference between revisions of "C/C++"

From HCL
Jump to: navigation, search
(General)
Line 23: Line 23:
 
* 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. Therefore, never do this:
 
* 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. Therefore, never do this:
 
<source lang="C">
 
<source lang="C">
int size;
+
  int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
+
  MPI_Comm_size(MPI_COMM_WORLD, &size);
char names[size][MPI_MAX_PROCESSOR_NAME];
+
  char names[size][MPI_MAX_PROCESSOR_NAME];
 
</source>
 
</source>
 +
* Implement delays in the execution of the program with help of [http://linux.die.net/man/2/nanosleep nanosleep]. Compared to sleep and usleep, nanosleep has the advantage of not affecting any signals, it is standardized by POSIX, it provides higher timing resolution, and it allows to continue a sleep that has been interrupted by a signal more easily.

Revision as of 15:57, 1 February 2011

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++

General

  int size;
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  char names[size][MPI_MAX_PROCESSOR_NAME];
  • Implement delays in the execution of the program with help of nanosleep. Compared to sleep and usleep, nanosleep has the advantage of not affecting any signals, it is standardized by POSIX, it provides higher timing resolution, and it allows to continue a sleep that has been interrupted by a signal more easily.