如何判断main函数是否被调用?

发布于 2024-11-13 06:31:09 字数 806 浏览 7 评论 0原文

我想编写一些依赖于静态变量值的代码。所以我想添加一些检查以消除从其他静态变量构造函数调用此代码的可能性。并永远防止静态初始化顺序惨败。例如:

static Foo foo = Foo();

// this function should be called ONLY from main program conrol flow
// ONLY after all static variable initialization was complete! ONLY!
int bar()
{
#ifdef _DEBUG
  if(! CRT_was_initialized_and_main_function_was_called ) ShowErrorMessage();
#endif
  if(foo.somefunction() == 2) return 0; else return -1;
}

//here inattentive programmer will caught error message during debug
const int barConstant = bar();

int main()
{
  //now all is fine
  const int barConstant = bar();
}

我该怎么做?如何检查我的函数是否在主函数之后被调用?

更新: Foo 对象有一个非常繁重的初始化代码,它可能会很慢,甚至抛出异常

更新2:这样做没有生命危险。栏前评论功能在大多数情况下都运行良好。我对某种调试检查感兴趣,以惩罚程序调试版本中不专心的程序员,而不是手动执行此操作。它可能是非标准的方式,比如调用一些只能在 MSVC 上运行的疯狂内置函数。

I want to write some code which will depend on a static variable value. So I want to add some check to eliminate the possibility of calling this code from other static variable constructors. And prevent the static initialization order fiasco once and forever. For example:

static Foo foo = Foo();

// this function should be called ONLY from main program conrol flow
// ONLY after all static variable initialization was complete! ONLY!
int bar()
{
#ifdef _DEBUG
  if(! CRT_was_initialized_and_main_function_was_called ) ShowErrorMessage();
#endif
  if(foo.somefunction() == 2) return 0; else return -1;
}

//here inattentive programmer will caught error message during debug
const int barConstant = bar();

int main()
{
  //now all is fine
  const int barConstant = bar();
}

How can I do so? How to check whether my function was called after main function was?

UPDATE: Foo object have a very heavy initialization code, it could be slow and even throw an exception

UPDATE2: There is no life concern to do so. Comment before bar function worked fine most of the time. I interested in some sort of debug check to punish inattentive programmer in debug version of program instead of doing this manually. And it could be nonstandard way like a call to some crazy builtin function which will work only on MSVC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕梦 2024-11-20 06:31:09

创建一个全局 bool 标志,指示 main 是否被调用,最初为 false。
main() 内将其更改为 true,并且仅在此处更改。这不是一个优雅的解决方案,但也是一个非常奇怪的问题的解决方案。

Create a global bool flag that indicates whether main was called or not, initially false.
Change it to true inside main(), and only change it there. Not an elegant solution, but a solution to a very weird problem, too.

画离情绘悲伤 2024-11-20 06:31:09

我会使用这个函数,并且 mainCalled 是一个全局布尔变量,默认情况下为 false,并在调用 main 时更改为 true:

int bar()
{
    static bool called = 0;
    if (!mainCalled)
        ShowErrorMessage();
    if (called)
    {
        return -1;
    }
    called = true;
    //do somthing
    return 0;
}

i would go with this function and mainCalled is a global boolean variable what is false by default and changed to true whever main is called:

int bar()
{
    static bool called = 0;
    if (!mainCalled)
        ShowErrorMessage();
    if (called)
    {
        return -1;
    }
    called = true;
    //do somthing
    return 0;
}
栀梦 2024-11-20 06:31:09

您可以简单地在函数之前声明一个全局变量int main_known = 0;

在函数中检查变量是否为 0,并在 main 中将变量设置为 1。

You can simply declare a global variable int main_called = 0; before the function.

In the function you check if the variable is 0 and in main you set the variable to 1.

撑一把青伞 2024-11-20 06:31:09

没有一种可移植的方法可以做到这一点。当然,您可以拥有自己的全局变量,在 main 中初始化,然后在 main 末尾取消初始化(不要忘记使用构造函数/析构函数来确保即使在异常退出 main 时也能正常工作)。

在 MSVC 上,一种更简单但不可移植的方法是#prgama init_seg。这为您提供了三个预设阶段以及您可以自己创建的更多阶段。

马丁

There isn't a portable way to do this. You can of course have your own global that you initialise in main and then uninitialise at the end of main (don't forget to use a constructor/destructor to ensure this works right even when an exception exits main).

On MSVC an easier but non portable way is #prgama init_seg. This gives you three pre-set phases and many more you can create yourself.

Martyn

咆哮 2024-11-20 06:31:09

对于单例模式来说,这不是一个可以检查其自身初始化的好例子吗?

Wouldn't this be a good case for a singleton pattern than can check its own initialization?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文