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?

Developer technologies | C++
Developer technologies | Visual Studio | Other
Community Center | Not monitored
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.