Main Content

Expensive unused object

Expensive local object is constructed but not used

Since R2024a

Description

This defect occurs when you construct an expensive local object using its default constructor but do not use the object. The list of objects that Polyspace® considers expensive includes:

  • Standard containers such as std::vector, std::list, std::stack, std::queue, std::deque, std::set, std::map, std::unordered_ser, and std::unorder_map

  • String classes such as basic_string and basic_stringstream

Polyspace considers a local object to be unused if both of these conditions are true:

  • The object is either not initialized or initialized with constant data.

  • Your code does not read the object after construction.

Polyspace assumes that the default constructors of these objects have no side effects. Polyspace does not report this defect if you construct an expensive object using a user-defined constructor, which can have side effects.

Risk

Constructing and destroying expensive objects reduces the efficiency of your code. Compilers generally do not detect if constructed objects remain unused. Such expensive unused objects make your code unnecessarily inefficient.

Fix

To fix this defect, review your code. Either revise your code so that the expensive unused object is used, or remove its declaration.

Performance improvements might vary based on the compiler, library implementation, and environment that you are using.

Examples

expand all

In this example, the function findPrevElement() searches the vector of strings vec for a substring. The expected behavior of the function is that it returns the element preceding the first element that contains substring. The string previousElement is declared but never read in findPrevElement(). Because previousElement is a string, the compiler does not detect the unused expensive object. Polyspace reports a defect.

#include <vector>
#include <string>

std::string findPrevElement(const std::vector<std::string>& vec, const std::string& substring) {
    std::string previousElement;

    for (const auto& str : vec) {
        if (str.find(substring) != std::string::npos) {
            return str;
        }
        
    }

    return "";  // Return an empty string if no element contains the substring.
}
Correction — Review Code and Use String

To fix this defect, review and revise your code so that unused objects are used. In the preceding implementation, the function findPrevElement() does not return the element preceding the first element that contains substring. The function returns the element that contains substring. The correct implementation uses the string previousElement and Polyspace does not report a defect.

#include <vector>
#include <string>

std::string findPrevElement(const std::vector<std::string>& vec, const std::string& substring) {
    std::string previousElement;

    for (const auto& str : vec) {
        if (str.find(substring) != std::string::npos) {
            return previousElement;
        }
        previousElement = str;
    }

    return "";  // Return an empty string if no element contains the substring
}

Result Information

Group: Performance
Language: C++
Default: Off
Command-Line Syntax: EXPENSIVE_UNUSED_OBJECT
Impact: Low

Version History

Introduced in R2024a