TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OAL#011Checked automatically with code checker

Synopsis:Use smart pointers for memory management
Language:C++
Severity Level:2
Category:Object Allocation


Description:

Smart pointers are pointers that free their allocated memory automatically when they go out of scope. In this way, memory leaks are avoided automatically.

Example (wrong):

void m() {
  MyObject* ptr = new MyObject(); 
  ptr->DoSomething(); // Use the object in some way
  delete ptr; // Destroy the object. Done with it.
}

Example (correct):

void m() {
  std::unique_ptr<MyObject> ptr(new MyObject());
  ptr->DoSomething();
}