简体   繁体   English

C-检查是否分配了整数

[英]C - Check if Integer is assigned

How do I determine if an integer is unassigned? 如何确定是否未分配整数?

int i; /* no assignment */

if (/* conditional statement here to check if int i is unassigned or not */) {
   printf("Integer is unassigned!\n");
} else {
   printf("Integer is assigned!\n");
}

You can't. 你不能 It will have "undefined" content, meaning it'll contain what ever happens to be in that memory location at that time. 它将具有“未定义”的内容,这意味着它将包含当时该内存位置中的所有内容。

. . . unless i is declared at the global scope, then it will be initialised to Zero. 除非在全局范围内声明了i,否则它将被初始化为零。

C doesn't intrinsically support this - just like it doesn't intrinsically support bounds checking on arrays. C本质上不支持此功能-就像它本质上不支持对数组进行边界检查一样。 It's a trade-off between speed/efficiency and safety. 在速度/效率和安全性之间进行权衡。

In general... initialize your variables. 通常...初始化变量。

It's very simple. 非常简单 You know it is unassigned because you did not initialise it. 您知道它尚未分配,因为您没有初始化它。

如果i是全局或静态的,则其值将为0 ,否则它的值可以为任何值,并且无法找出是否为垃圾。

You may be able to ask for compiler warnings if you use uninitialized values. 如果使用未初始化的值,您也许可以要求编译器警告。 They're not wholly reliable, however - you get the occasional false positive where the DFA isn't as clever as you'd hope, and maybe occasional false negatives (I'd hope not, but I promise nothing). 但是,它们并不是完全可靠的-在DFA不如您希望的那样聪明的情况下,您偶尔会收到误报,并且可能会偶尔出现误报(我希望不会,但我不承担任何责任)。

For GCC: 对于GCC:

-Wuninitialized -O1

If you want to write conditional code: 如果要编写条件代码:

int a = 3;
int b;
int *p = (rand() > RAND_MAX/2) ? &a : &b;
if (is_uninitialized(*p)) // blah

then you're out of luck. 那你就不走运了 Unlike some dynamic languages, C has no concept of "the undefined value". 与某些动态语言不同,C没有“未定义的值”的概念。 If a variable is not initialized, it isn't given some special value which can be tested for later. 如果未初始化变量,则不会为它提供一些特殊的值,以便以后进行测试。 It's not given a value at all, so it's undefined what happens when you use the variable. 它根本没有赋值,因此使用该变量时会发生什么是不确定的。

As others have noted, you can't write a C program that detects if one of its own variables is uninitialized, and you should strive to make sure that variables are always initialized. 正如其他人指出的那样,您不能编写一个C程序来检测其自己的变量之一是否未初始化,而应努力确保始终对变量进行初始化。

  • If your goal is to make sure all variables are initialized, a tool like valgrind can detect uses of uninitialized variables dynamically , through expensive run-time analysis. 如果您的目标是确保所有变量都已初始化,则类似valgrind的工具可以通过昂贵的运行时分析来动态检测未初始化变量的使用。

  • If your goal is to make sure that private data is initialized exactly once, the usual method is to protect it with 如果您的目标是确保私有数据仅被初始化一次,通常的方法是使用

     int i; static bool initialized = 0; ... if (!initialized) { initialized = 1; i = ... i's initial value ...; } 

Usually variables are set to 0 by the C library, but not necessarily. 通常,C库会将变量设置为0,但这不是必须的。

But basically, you can't. 但基本上,您不能。 Assign them a default value in the definition, for instance: 在定义中为其分配一个默认值,例如:

int i = 0; /* Or what ever value you know won't be used elsewhere */

Then if you run some code and want to check if the value was set there, you can compare to your initial value. 然后,如果您运行一些代码并想要检查该值是否在此处设置,则可以与您的初始值进行比较。

As all previous answers, there is no way to detect that at runtime. 正如所有先前的答案一样,没有办法在运行时检测到它。 However, almost any static code analysis tool with warn you for unassigned variables. 但是,几乎所有带有静态代码分析工具的工具都会警告您未分配的变量。

Using a variable before intialization (or assignment) is serious cause of errors. 在初始化(或分配)之前使用变量是导致错误的严重原因。 You can not reliably check it at runtime, but you can detect it during or before compilation. 您无法在运行时可靠地对其进行检查,但是可以在编译期间或编译之前对其进行检测。

I suggest not to check it inside the code. 我建议不要在代码中检查它。 Because this is likely to cause compiler warnings (Variable 'i' is used before it has been assigned a value), introduce new errors and has very little chance on succes in medium to large programs. 因为这可能会引起编译器警告(在给变量赋值之前使用变量“ i”),所以会引入新的错误,并且在大中型程序中成功的可能性很小。

The best method is to use static code analys tools (like QA/C or PCLint). 最好的方法是使用静态代码分析工具(例如QA / C或PCLint)。 Using compiler at high warning sensitivity level is a free option, with much less coverage as the specialized tools. 在高警告敏感度级别使用编译器是一个免费选项,与专用工具相比,其覆盖范围要小得多。

If you perform code reviews, you can also include a check for uninitialized variables on the checklist. 如果执行代码审查,则还可以在清单上包括对未初始化变量的检查。 This is no guarantee, but it will trigger manual checks from reviewers. 这不是保证,但会触发审阅者的手动检查。

If it is runtime checking you want, then you can start of by intializing variables to an out-of-range value. 如果要进行运行时检查,则可以通过将变量初始化为超出范围的值来开始。 For instance -1 for an otherwise postive value. 例如-1,表示正值。 Then you can check for 然后您可以检查

#define UNASSIGNED_VALUE -1
    static int number_of_apples = UNASSIGNED_VALUE;
    if (UNASSIGNED_VALUE == number_of_apples)
    {
       // error handling
    }

this is not a true 'unintialized' variable, but at least you can detect whether runtime assignments in legal range were done. 这不是真正的“非初始化”变量,但是至少您可以检测到是否在合法范围内完成了运行时分配。

In C, an integer takes on an undefined value when it is created. 在C语言中,整数在创建时采用未定义的值。 This means that if your first use of that integer comes from a register/memory location/device with a 5893872 in it, that's the value of that integer. 这意味着,如果您第一次使用该整数来自其中具有5893872的寄存器/内存位置/设备,则该整数就是该整数的值。 (Mileage varies for debug/release compilation.) (调试/发布编译的里程会有所不同。)

The usual method of dealing with this is to use a nonsensical default: 解决此问题的常用方法是使用无意义的默认值:

int number_of_widgets = -1;

...or a flag to indicate its state: ...或用于指示其状态的标志:

int number_of_widgets;
int number_of_widgets_assigned = 0;

if (number_of_widgets_assigned)
  do something
else
  do something else
number_of_widgets_assigned = 1;

There is no other way to detect whether something has been assigned to - unless you want to get into the debugging features of your hardware and I suspect that is not what this conversation is about. 没有其他方法可以检测是否已分配了某些内容-除非您想了解硬件的调试功能,而且我怀疑这与本次对话无关。

Checking whether or not a variable you're using is initialized (assigned) at runtime is notoriously difficult for C. There's no language support for it, and the information available at runtime simply is insufficient for perfect detection of uninitialized values. 对于C而言,检查所使用的变量是否在运行时进行初始化(赋值)是非常困难的。对于C语言来说,它没有语言支持,并且在运行时可用的信息不足以完美检测未初始化的值。 Dynamic analysis tools such as Valgrind/Memcheck goes through great lengths (such as keeping track of every byte of memory in your process's address space and then examining every store to mark a byte as intiialized ) to determine whether or not the value using is initialized and are still susceptible to false positives. 动态分析工具(如Valgrind / Memcheck)经过很长的时间(例如跟踪进程地址空间中的每个内存字节,然后检查每个存储以将字节标记为initiialized),以确定use的值是否已初始化和仍然容易出现误报。

If you're just trying to minimize such errors in your programs, static analysis tools such as lint can do a reasonably good job of informing you of whether or not you're using uninitialized variables. 如果您只是想最大程度地减少程序中的此类错误,则诸如lint之类的静态分析工具可以很好地通知您是否正在使用未初始化的变量。 In fact, I believe most compilers will do their best to tell you when you're doing this (though, they're certainly not perfect.) 实际上,我相信大多数编译器会尽力告诉您何时进行此操作(尽管它们当然并不完美)。

In C# I'd use: 在C#中,我将使用:

Nullable<int> i = null; /* null assignment */

if (i == null) {
   printf("Integer is unassigned!\n");
} else {
   printf("Integer is assigned!\n");
}

Not sure if this'd translate to C, though. 不过,不确定是否将其转换为C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM