C++ style guide: Difference between revisions

Jump to navigation Jump to search
m
Add section on new/delete keywords
m (Add section on new/delete keywords)
Line 325: Line 325:
   foo (str);
   foo (str);
}
}
</syntaxhighlight>
|}
=== new/delete ===
Pointers that will be allocated memory with {{codeline|new}} should be initialized with the C++ literal {{codeline|nullptr}}, not the numerical value 0 or the macro {{codeline|NULL}}.
The {{codeline|delete}} keyword accepts {{codeline|nullptr}} and programmers should not put an {{codeline|if (ptr)}} guard around {{codeline|delete}}.
{| class="wikitable"
! style="color:green;" | good
! style="color:darkred;" | bad
|-
| <syntaxhighlight lang="c++">
delete ptr;
</syntaxhighlight>
| <syntaxhighlight lang="c++">
if (ptr)
  delete ptr;
</syntaxhighlight>
</syntaxhighlight>
|}
|}
1,072

edits

Navigation menu