Skip to content

Instantly share code, notes, and snippets.

@blockspacer
Last active February 26, 2021 17:55
Show Gist options
  • Save blockspacer/c44b00fa355c920efcb2abfa84e3990f to your computer and use it in GitHub Desktop.
Save blockspacer/c44b00fa355c920efcb2abfa84e3990f to your computer and use it in GitHub Desktop.
Dangers you can encounter when using C++.

About

Dangers you can encounter when using C++.

Calling virtual functions from a constructor or destructor is dangerous and should be avoided whenever possible.

Uninitialized data (Initialization in C++ is bonkers)

Solutions:

Virtual destructor

Integer division

Floating point comparison

= vs ==

Mixing signed and unsigned values

delete vs. delete[]

Switch statements without break

this in constructor or destructor

zero divizion

“static initialization order ‘fiasco’

UB when accessing another field, then one which was set

overflow

Order of evaluation

foobar(foo(), bar()); It is not defined, whether foo() will be called before or after bar() has returned. That’s just silly. Also: x=x++ undefined

Using smart pointers, such as auto_ptr, unique_ptr, shared_ptr, with arrays is also incorrect

https://www.toptal.com/c-plus-plus/top-10-common-c-plus-plus-developer-mistakes

Allowing Exceptions to Leave Destructors

https://www.toptal.com/c-plus-plus/top-10-common-c-plus-plus-developer-mistakes

Passing an Object by Value (memory)

https://www.toptal.com/c-plus-plus/top-10-common-c-plus-plus-developer-mistakes

Auto Type Conversion / forgotten explicit

https://devblogs.microsoft.com/oldnewthing/20060524-12/?p=31083

Lifetime

Solutions:

  • https://www.youtube.com/watch?v=VynWyOIb6Bk This is an experience report of the Clang-based implementation of Herb Sutter's Lifetime safety profile for the C++ Core Guidelines, available online at cppx.godbolt.org.

spurious wakeup (threads & condition_variable)

TODO

Want more?

Report crash to server

record metrics

task runners

callbacks

Abstractions and Data Structures

refl

Use -Wlifetime from https://github.com/mgehre/llvm-project (lifetime branch)

add Lifetime Safety Profile in Clang -Wlifetime -Wlifetime-pointer-arithmetic

Bad Syntax? Solutions?

@blockspacer
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment