重置静态变量 [英] Static variables getting reset

查看:225
本文介绍了重置静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Guys&加尔斯,

这是我上一个项目的延续(请参见此处) .感谢您的大力帮助,我们得以将DLL成功拆分为多个模块.但是,出现了更多问题!

无论如何,旧代码具有大量的静态变量,函数和外部元素,有些则不太合适.当它们作为一个模块存在时,它们都工作良好,但是将它们分离后,它们中的大多数不再起作用,并且有时会重置这些值.

有人遇到过这个吗?您是如何解决它们的?

Hi Guys & Gals,

This is a continuation from my previous project (See here). Thanks to much help from you, we were able to successfully rip apart the DLL into multiple modules. However, more problems came up!

Anyhow, the old code had an abundance of static variables, functions and externs, some appropriate some not so much. They all worked well when they existed as one module, but after separating them, most of them are no longer functional, and the values sometimes gets reset.

Has anyone encountered this? How did you solve them?

Thanks!

推荐答案

您说您已经分离了包含静态变量的模块.
这很明显,但是如果您有一个文件,
static int my_static;
然后您有机械性"的提示,例如不费吹灰之力,将代码分为两个文件,两个文件都具有:
static int my_static;
那么您在内存中有两个"int"变量,而不是一个,并且如果您在文件#1中初始化my_static,则由于它们是完全独立的,因此您不会初始化文件#2 my_static变量.
最终结果从表面上看就像是静态变量"reset"(如您所述).
以防万一,请通过删除static关键字并将变量声明为extern(除一个文件之外的所有文件)来解决此问题.
You say that you have separated modules that contained static variables.
This is kind of obvious, but if you have one file with
static int my_static;
and then you have "mechnically", e.g. without much thinking, partitioned code into two files, both with:
static int my_static;
then you have two "int" variables in memory instead of one, and if you initialize my_static in file #1, you are not initializing file #2 my_static variable as they are completely independent.
The net result would look, on the surface, as if (as you mentioned), static variables "resetting".
Just in case if this is the case, fix it by removing static keyword and declaring variables as extern in all but one file.


仅适用于某人的旧代码.如果您自己(或者即使没有这样做)也要洗手.抱歉,污垢是污垢,如果没有辛苦和令人不快的工作,它就去不了.

但是,根据您的编译器的不同,将警告级别提高到最大程度可以帮助检测某些无效代码,尽管可能不完整.

有不同的代码分析工具.尝试找到一些:

http://en.lmgtfy.com/?q= {find + OR + detect + OR + remove)+(unused + OR + dead)+ code + automatically +%22C%2B%2B%22 [ ^ ]

我不知道您使用的平台,编译器等,因此查看搜索结果,超过一千万,列表顶部看起来很相关.

—SA
Only in someone''s legacy code. If you did it yourself (or even if you did not) — get your hands dirty. Sorry, dirt is dirt, it cannot go without hard and unpleasant work.

However, depending on your compiler, increase in warning level to maximum can help to detect some dead code, albeit not completely.

There are different code analysis tools. Try to find some:

http://en.lmgtfy.com/?q=(find+OR+detect+OR+remove)+(unused+OR+dead)+code+automatically+%22C%2B%2B%22[^]

I don''t know you platform, compiler, etc., so see the search results, there are over 10 million, top of the list looks relevant.

—SA


首先,很难不看代码就很难说.如果您仔细阅读说明,就会提到许多静态变量无法正常工作.好吧,您需要问自己的第一个问题,是否要让它们保持静态?为什么?拆开后,希望像SA所说的那样弄脏您的手.

您不仅要分解它,还可能要质疑某些功能.可能是您需要重构一些代码,在某些情况下,您需要合并代码/功能.

查找编译器警告,它们可以为您提供一些提示,并且您可能希望对代码进行检测,以了解运行时正在发生的情况.祝你好运!
First off, it is very hard to say with out looking at the code. If you read your description closely, you mentioned that many of the static variables are not functional. Well, the first question you need to ask your self, do you want to keep them static? and Why? After breaking it apart, expect like SA mentioned it to get your hands dirty.

Not only do you want to break it apart you may have to question some of the functionality. May be you need to refactor some code and in some case you need to merge code/functionality.

Look for compiler warnings, they can give you some hints, and you may want to instrument your code to understand what is going on at run time. Good Luck!


这篇关于重置静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆