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

eugenern 0 Reputation points
2023-03-10T02:32:37.76+00:00

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.
4,699 questions
C++
C++
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.
3,562 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,530 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,281 Reputation points Microsoft Vendor
    2023-03-10T05:34:26.1433333+00:00

    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,

    Elya


    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.