Why don't I get warning C4101 (unreferenced local variable) for an unused local vector?

eugenern 0 Reputation points

In the project I'm working on, the compiler stops the build with C4101 if there's a local variable, such as an int, that never gets referenced. However, when I declare a local vector<int> and never mention it again, the build succeeds with no warning or error. Is this intended behavior? Does it have something to do with memory allocation?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
2,791 questions
Microsoft Build
Microsoft Build
A Microsoft platform for building applications with Visual Studio and .NET on Windows.
378 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,751 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 3,866 Reputation points Microsoft Vendor

    Hi eugenern,

    After testing, I encountered the same result as you, which will cause warning C4101. You only need to initialize local variables to solve this problem, or do not use uninitialized variables.

    Regarding why there is no warning for vector<int>, we can see that vector is a class, and class will automatically call the default constructor when defining variables, which will initialize it, so vector<int> will not cause this problem.

    User's image

    Best regards,


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.