C and C++ are very powerful languages, but you need to be careful when using them. Here are some crucial practices:
?inline
-Wall
-Wpedantic
-Wextra
for GCC and clang catch most common mistakes.
You can also use clang-tidy, IntelliSense, and other diagnostics (often integrated into IDEs).
?inline
scanf("%s", str)
, gets()
, and others can lead to mistakes like
buffer overflows.
Accessing arrays out of bounds can be caught in C++ by using .at(x)
instead of [x]
for some containers.
?inline
Since C++11, prefer smart pointers like
std::unique_ptr and
std::shared_ptr
over new
and delete
.
Without smart pointers, it's easy to forget to delete
(or to mess it up) and to leak memory.
If your program is crashing, and you don't know why, you can track down the issue using additional software: