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 1096431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:17:02+00:00 2026-05-17T00:17:02+00:00

I have a legacy C Linux application that I need to reuse . This

  • 0

I have a legacy C Linux application that I need to reuse . This application uses a lot of global variables. I want to reuse this application’s main method and invoke that in a loop. I have found that when I call the main method( renamed to callableMain) in a loop , the application behavior is not consistent as the values of global variables set in previous iteration impact the program flow in the new iteration.

What I would like to do is to reset all the global variables to the default value before the execution of the the new iteration.

for example , the original program is like this

OriginalMain.C

#include <stdio.h>

int global = 3; /* This is the global variable. */

void doSomething(){     
         global++; /* Reference to global variable in a function. */    
}    

     // i want to rename this main method to callableMain() and
     // invoke  it in a loop 
     int main(void){    
       if(global==3) {    
       printf(" All  Is Well \n");    

       doSomething() ;  
     }
     else{

       printf(" Noooo\n");  

       doNothing() ;

     }
     return 0;
}

I want to change this program as follows:

I changed the above file to rename the main() to callableMain()

And my new main methods is as follows:

int main(){  

     for(int i=0;i<20;i++){  

         callableMain();

         // this is where I need to reset the value of global vaiables
        // otherwise the execution flow  changes
     }    
}   

Is this possible to reset all the global variables to the values before main() was invoked ?

The short answer is that there is no magical api call that would reset global variables. The global variables would have to be cached and reused.

  • 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-05-17T00:17:02+00:00Added an answer on May 17, 2026 at 12:17 am

    I think you must change the way you see the problem.

    Declare all the variables used by callableMain() inside callableMain()’s body, so they are not global anymore and are destroyed after the function is executed and created once again with the default values when you call callableMain() on the next iteration.

    EDIT:

    Ok, here’s what you could do if you have the source code for callableMain(): in the beginning of the function, add a check to verify if its the first time the function its being called. Inside this check you will copy the values of all global variables used to another set of static variables (name them as you like). Then, on the function’s body replace all occurences of the global variables by the static variables you created.

    This way you will preserve the initial values of all the global variables and use them on every iteration of callableMain(). Does it makes sense to you?

    void callableMain()
    {
      static bool first_iter = true;
    
      if (first_iter)
      {  
        first_iter = false;
        static int my_global_var1 = global_var1;
        static float my_global_var2 = global_var2;
        ..
      }
    
      // perform operations on my_global_var1 and my_global_var2, 
      // which store the default values of the original global variables.
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy C++ application that uses DDS for asynchronous communication/messaging. I need
I have a legacy VB6 application that was built using MSDE. As many client's
I have a legacy C++ module that offers encryption/decryption using the openssl library (DES
We have some legacy code that needs to identify in the Page_Load which event
I have this legacy code base (Compaq PERL) , about 1500 lines of code,
I have a legacy MS Access 2007 table that contains 52 fields (1 field
I have legacy asp.net 1.1 website. It has a very poor VB layered achitecture.
I have a legacy WinForms Mdi App in VB.Net 2.0 which I am adding
We have a legacy ASP.net powered site running on a IIS server, the site
I have a legacy VB6 program which installs an Access file in a sub-directory

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.