What's the purpose of using braces (i.e. {}) for a single-line if or loop?

前端 未结 23 1445
独厮守ぢ
独厮守ぢ 2020-11-28 00:33

I\'m reading some lecture notes of my C++ lecturer and he wrote the following:

  1. Use Indentation // OK
  2. Never rely on operator preced
23条回答
  •  无人及你
    2020-11-28 01:34

    wrt 6: It's safer because deleteing a null pointer is a no-op. So if you happen to accidentally go through that path twice, you won't cause memory corruption be freeing memory that is either free or has been allocated to something else.

    This is most of an issue with static file scope objects and singletons that have not very clear lifetimes and have been known to get recreated after they've been destroyed.

    In most cases, you can avoid the need for this by using auto_ptrs

提交回复
热议问题