GCC -Wuninitialized不警告有关未初始化的结构 [英] GCC -Wuninitialized not warning about uninitialized structs

查看:233
本文介绍了GCC -Wuninitialized不警告有关未初始化的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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 ++-未初始化-O2 test.cpp不会警告tm尚未初始化.Valgrind会一直这样做,直到添加了memset行.在Linux上的strptime的手册页上说应该对其进行初始化,而在我进行初始化之前,我在更复杂的程序上看到了随机的日期.在这种情况下,是否有任何会发出警告的GCC标志?

g++ -Wuninitialized -O2 test.cpp doesn't warn about tm not having been initialized. Valgrind does until the memset line is added. the Man pages for strptime on Linux say it should be initialised and I was seeing randomized dates on a more complicated program until I did initialise it. Are there any GCC flags that will produce a warning in these circumstances?

推荐答案

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

GCC can't look into the already compiled code of the strptime, mktime and ctime functions at compile time. You just pass the address of the struct, from the point of the call, without reading anything. Valgrind on the other hand executes your program and tracks all the memory and will check up whether there is a read before a write of a particular memory block and can thus tell you.

如果这些函数将在头文件中内联定义,则编译器可以内联它们并将指针地址追溯到未初始化的结构.不过,我还没有测试过GCC的表现如何(或者就此而言,一般来说是编译器).

If those functions would be defined inline in the header, you could have a chance that the compiler could inline them and track back the pointer address back to the uninitialized struct. I haven't tested how good GCC is at that, though (or for that matter, compilers in general).

这篇关于GCC -Wuninitialized不警告有关未初始化的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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