CRITICAL
Rule Definition
If you allocate memory dynamically, at one time, you will have to deallocate it to avoid memory leaks. If you return the pointed-to data, it means you have not deallocated it within the function. And since you don't return the pointer itself, there will be no way for the function caller to deallocate the memory.
Remediation
You can correct this problem by using a smart pointer that will automatically reclaim the memory at the function end (replace 'unique_ptr' with another smart pointer, depending on what smart pointer is available to you)
Violation Code Sample
int f()
{
int *i = new int(42);
return *i;
}
Fixed Code Sample
int f()
{
unique_ptr<int> i (new int(42));
return *i;
}
Reference
High Integrity C++ Coding Standard Manual
Related Technologies
C++
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.