[英]C - how to protect a a variable from being accidentally changed after a certain point in the function
C - how to protect a variable from being accidentally changed after a certain point: C - 如何保护变量在某一点之后不会被意外更改:
For example, in a C program, I declared an integer and changed it's value several times in the first part of the program. 例如,在C程序中,我声明了一个整数,并在程序的第一部分中多次更改了它的值。 But then after a certain point I want to keep its value from being accidentally changed, or at least get a warning when any attempt to change its value happens. 但是在某一点之后我想保持其价值不被意外改变,或者至少在任何改变其价值的尝试发出警告时都会发出警告。 How do I do that? 我怎么做?
You can't. 你不能。
A possible solution would be to move the part of the function that modifies the variable into a separate function. 一种可能的解决方案是将修改变量的函数部分移动到单独的函数中。 That function would return the value of the variable which can be used to initialize a const
variable: 该函数将返回可用于初始化const
变量的变量值:
int determine_value_of_a()
{
int result = 0;
/* Modifications of result. */
return result;
}
void f()
{
const int a = determine_value_of_a();
}
If using preprocessor macros is acceptable, then you can do this: 如果使用预处理器宏是可以接受的,那么您可以这样做:
{ // Function body starts here.
int x = …;
… // Change x as desired here.
const int y = x;
#define x y;
… // “x” is actually the const y here.
#undef x
}
Generally, though, I would not recommend it. 但一般来说,我不推荐它。 If you cannot trust the body of a single function to do things correctly, then the function is too complicated and ought to be restructured, as by calling another function to provide the value of the object. 如果你不能信任单个函数的主体来正确地执行操作,那么函数太复杂并且应该重新构造,例如通过调用另一个函数来提供对象的值。
Put the variable in a opaque struct with an indication of whether it can be changed. 将变量放在不透明的结构中,并指示是否可以更改它。 Access the variable, both for getting and changing the value, from a function. 从函数中访问变量,以获取和更改值。 If the indication for changeability if off, the function does not allow changes 如果关闭时可更改性指示,则该功能不允许更改
#include "swinger.h"
int main(void) {
struct moodswinger *var;
var = newswinger();
setvar(var, 42);
getvar(var);
swing(var, 0);
setvar(var, -1); /* oops */
}
The file swinger.h
declares the functions. swinger.h
文件声明了这些函数。
The file swinger.c
defines the struct and code. swinger.c
文件定义了结构和代码。
int x;
x = …; // This is okay.
{
const int y = x; // Make a copy of x.
{
const int x = y; // Make a const copy of x that hides the original.
/* The compiler should produce a diagnostic if you
attempt to modify x here.
*/
}
}
Or, if you do not want the indenting to change: 或者,如果您不希望缩进更改:
int x;
x = …;
{ const int y = x; { const int x = y;
…; // x is const here.
} }
The only way you could do that would be 你能做到的唯一方法就是
(a) to use a function to initialize the variable.. (a)使用函数初始化变量..
int main( int argc, char** argv )
{
const int myVar = initMyVar();
...
}
or (b) pass the variable into another function that does the other work... 或者(b)将变量传递给另一个完成其他工作的函数......
void myWorkerFunc( const int myVar )
{
...
}
int main( int argc, char** argv )
{
int myVar = initMyVar();
...
myWorkerFunc( myVar );
}
Set a conditional breakpoint on it in your debugger. 在调试器中为其设置条件断点。 Then you will know if it ever changes. 然后你会知道它是否会发生变化。 You could also write a "watchdog" type thread that scans your var every so often and warns on change, but that won't be immediate. 您还可以编写一个“看门狗”类型的线程,每隔一段时间扫描一次var并发出变化警告,但这不会立即发生。
Neither of these are within the realm of C
directly, but would help you determine if your var was erroneously changed. 这些都不是直接在C
范围内,但可以帮助您确定您的var是否被错误地更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.