Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8589845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:09:15+00:00 2026-06-11T23:09:15+00:00

Compilers detect unused variable within the scope of a function. However, I found there

  • 0

Compilers detect unused variable within the scope of a function. However, I found there are many variables, defined inside a structure, which are never read (but may have been written many times). Is there any tool/analyzer or even compiler flags to detect such unused variables?

Example:
For example, in the following structure:

typedef struct jj_t {
    int count;
    int *list;
} jj;

Analyzer may find that count is never read anywhere in the code.

My analyze of my code, shows this frequently happens! This was my fault, but it maybe the common case for the applications developed by different users over the years. Removing these variable may significantly reduces memory usage. I just need a tool for detecting such variables and I will manually remove them.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:09:16+00:00Added an answer on June 11, 2026 at 11:09 pm

    I can give one solution.

    But:

    1. The effort is probably much bigger than checking by hand. Almost every good IDE for programmers allows you to see all references to a given variable.

    2. This probably won’t work in every case, you’ll need to specialize for some types.

    3. This will be collected by single program run.

    The idea is to wrap your data types. With such encapsulation you can count every read operation.
    See:

    template <class T, class Parent, int NO=1>
    class TReadDetector {
    public:
       struct Data {
          bool touched;
          Data () : touched(false) {}  
          ~Data ()  {
            if (!touched) 
              std::cerr << typeid(*this).name() << ": not read!!!\n" << std::endl;
          }  
       };
       static Data  data;
       TReadDetector () {}
       TReadDetector (const T& t) : t(t) {}
       operator T () const {    data.touched = true; return t; }
       TReadDetector& operator = (const T& t) { this->t = t; }
    private:
       T t;
    };
    
    template <class T, class Parent, int NO>
    typename TReadDetector<T,Parent,NO>::Data  
                           TReadDetector<T,Parent,NO>::data;
    

    And usage:

    Instead of:

    struct A {
      int a;
      int b;
    };
    

    DO this:

    struct A {
      TReadDetector<int,A, 1> a;
      TReadDetector<int,A, 2> b;
    };
    
    
    int main() {
      A a;
      a.a = 7;
      a.b = 8;
      std::cout << a.a << std::endl;
      std::cout << TReadDetector<int,A, 1>::data.touched << std::endl;
      std::cout << TReadDetector<int,A, 2>::data.touched << std::endl;
      std::cout << "main() ended" << std::endl;
    };
    

    It will results in:

    7
    1
    0
    main() ended
    N13TReadDetectorIi1ALi2EE4DataE: not read!!!
    

    Notice last line printed after main(). You can collect this data to some external file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a compile-time way to detect / prevent duplicate values within a C/C++
Is there a flag or other reliable method to detect if a compiled C++
Are there any C or C++ compilers out there that implement aggressive memory consistency
How does a compiler detect duplicate definition across translation unit. Suppose there were a
Is there a way to detect (for instance with compiler warning) if classes are
I've problems to detect if there are new rows in a DataTable Email_Total that
Is there any way to detect underflow automatically during execution? Specifically I believe there
Are there any predefined constants in the C# compiler to detect what version of
I found the need to use String.format in my project. However, this doesn't work
I created function to detect the constness and l(r)valueness of the argument. template<class T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.