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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:18:15+00:00 2026-05-15T09:18:15+00:00

This is the way I get to prevent funA,funB,funC, etc.. for being used before

  • 0

This is the way I get to prevent funA,funB,funC, etc.. for being used before init

#define INIT_KEY 0xC0DE //any number except 0, is ok

static int initialized=0;

int Init()
{
  //many init task
  initialized=INIT_KEY;
}


int funA()
{
 if (initialized!=INIT_KEY) return 1;
 //..
}

int funB()
{
 if (initialized!=INIT_KEY) return 1;
 //..
}

int funC()
{
 if (initialized!=INIT_KEY) return 1;
 //..
}

The problem with this approach is that if some of those function is called within a loop
so “if (initialized!=INIT_KEY)” is called again, and again, although it’s not necessary.

It’s a good example of why constructors are useful haha, If it were an object I would be sure that when it was created, initialization was called, but in C, I don’t know how to do it.

Any other ideas are welcome!

  • 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-15T09:18:16+00:00Added an answer on May 15, 2026 at 9:18 am

    Use pointer to function.

    At construct time, point the functions at the function
    that does the required initialization, then updates the function pointers to point to the actual functions that do the work.

    I have done this with arrays of pointer to member functions in a class. The class has an internal integer which says what state the object it in. The integer is used to subscript into the array of pointer to member functions… State 0 does the init. State 1, does the work, State 2 sets things back to state 1.

    It worked very cleanly.

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

Sidebar

Related Questions

Has anyone found a way to get around this? Or a better technique to
This is ASP classic, not .Net. We have to get a way to SFTP
I get this error: Can't locate Foo.pm in @INC Is there an easier way
Right let’s get this out the way first. Yes, I want to hide the
Is there a way to prevent the error "This webpage is not available." from
Is there any way to prevent a key like F1 from being pressed? After
I often use webservice this way public void CallWebservice() { mywebservice web = new
Why was C# designed this way? As I understand it, an interface only describes
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
In C++, I code this way: //foo.h class cBar { void foobar(); } //foo.cpp

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.