c++ - GCC -Wuninitialized 不警告未初始化的结构

标签 c++ struct initialization gcc-warning

#include <ctime>
#include <iostream>
#include <cstring>
int main()
{

struct tm tm ;
//memset(&tm, 0, sizeof(struct tm));
strptime("1 Jan 2000 13:00:00", "%d %b %Y %H:%M:%S", &tm);
time_t t =mktime(&tm);
std::cout << ctime(&t);
return 0;
}

g++ -Wuninitialized -O2 test.cpp 不会警告 tm 尚未初始化。 Valgrind 会一直执行直到添加 memset 行。 Linux 上 strptime 的手册页说它应该被初始化,在我初始化它之前,我在一个更复杂的程序上看到了随机日期。是否有任何 GCcflags会在这些情况下产生警告?

最佳答案

GCC 无法在编译时查看strptimemktimectime 函数的已编译代码。您只需从调用点传递结构的地址,而无需读取任何内容。另一方面,Valgrind 执行您的程序并跟踪所有内存,并检查在写入特定内存块之前是否有读取,从而可以告诉您。

如果这些函数将在 header 中内联定义,则编译器可能会内联它们并将指针地址追溯到未初始化的结构。不过,我还没有测试过 GCC 在这方面有多好(或者就此而言,一般来说是编译器)。

关于c++ - GCC -Wuninitialized 不警告未初始化的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4179345/

相关文章:

c++ - 如何使用初始化列表初始化 unordered_map<string, unordered_set<string>> 成员?

c - 在 C 中将 struct char 数组分配给 char 数组会出现 "incompatible types in assignment"错误

c# - 使用集合初始化程序初始化 WebControl 对象的 Attributes 属性

c++ - 如何调用模板类型的正确构造函数?

c++ - c++-try block 之后是否需要立即编写catch block ?

c++ - 加入特定的 boost 线程

c++ - 数组或 vector 中的元素改变C++

swift - 使用结构构建端点

C# 如何通过反射设置 StructLayoutAttribute.Pack?

PHP 通知 : Uninitialized string offset: 0