本文介绍了如何在Linux上的C中查找未初始化的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的C源代码有许多未初始化的变量.该代码在RHEL 6.4操作系统上.
My C source code has many unintialized variables. The code is on RHEL 6.4 operating system.
有没有办法找到所有未初始化的变量?
Is there a way to find all the uninitialized variables?
推荐答案
查找所有所有是不可能的.但是,有一些工具可以帮助您找到其中一些:
Finding all of them is impossible, in the mathematical sense (at least without false-positives). However, there are some tools to help find some of them:
- 打开编译器警告.对于
gcc
,这将是-Wuninitialized
,-Winit-self
和-Wmaybe-uninitialized
.请注意,您将需要在各种优化级别上进行尝试;您会在不同的-O
级别收到不同的警告.请注意,-Wmaybe-uninitialized
(顾名思义)可能会带来误报. - 对于未初始化的内存(如
malloc
等),可以使用valgrind
.这实际上需要运行程序. - 静态检查器,例如 splint .(感谢安迪·莱斯特的建议.)
- Turn on compiler warnings. With
gcc
, this would be-Wuninitialized
,-Winit-self
, and-Wmaybe-uninitialized
. Note that you will need to try this with various levels of optimization; you'll get different warnings at different-O
levels. Note that-Wmaybe-uninitialized
(as the name suggests) may give false positives. - For uninitialized memory (as in
malloc
, etc.), you can usevalgrind
. This actually requires running the program. - Static checkers such as splint. (Thanks to Andy Lester for this suggestion.)
这篇关于如何在Linux上的C中查找未初始化的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!