gcc较高版本的一些编译警告收集

最近整理了下代码警告问题。这里记录一下。

在以前某个项目上竟然用-w把gcc的警告给关闭了,怪不得编译代码完全没警告,多漂亮的代码!

1、未使用变量、未使用函数返回值,未使用变量:

 
  
warning: unused variable ‘ret’ [-Wunused-variable] int ret;
-->修改:删之。没有达到函数末尾:
 
  
warning: control reaches end of non-void function [-Wreturn-type] int open(const char* file) { if(foo==11) { return ok; } // --> 修改:此处要加上返回值 }
没有返回值:
 
  
warning: no return statement in function returning non-void [-Wreturn-type] int Open(char* file){}
--> 修改:加上返回值2、参数一致性,参数类型检查空字符串:
 
  
warning: zero-length gnu_printf format string [-Wformat-zero-length] sprintf(buffer, "");
类型不对:
 
  
warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘char*’ [-Wformat=] warning: format ‘%d’ expects argument of type ‘int*’, but argument 4 has type ‘short int*’ [-Wformat=] --> 修改:按printf格式来修改,unsigned long使用%lu。不要用%d来打印字符串指针。
有符号和无符号:
 
  
warning: pointer targets in passing argument 2 of ‘ll_foobar’ differ in signedness [-Wpointer-sign] expected ‘unsigned char *’ but argument is of type ‘char *’
有符号和无符号比较:
 
  
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
--> 修改:强制转换类型参数类型:
 
  
warning: passing NULL to non-pointer argument 2 of ‘void* memset(void*, int, size_t)’ [-Wconversion-null] memset(foobar, NULL, sizeof(foobar)); (C++中,NULL是指针,不是数值,而memset要求的是数值0)
--> 修改:用0来代替,或将NULL强转为INT。比较诡异:
 
  
warning: argument to ‘sizeof’ in ‘char* strncpy(char*, const char*, size_t)’ call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess] strncpy(ptr, buffer, sizeof(ptr));
用意本身是好的,用法却是错的,ptr是指针,sizeof指针只能得到4。类似的有:
 
  
warning: argument to ‘sizeof’ in ‘void* memset(void*, int, size_t)’ call is the same expression as the destination; did you mean to dereference it? [-Wsizeof-pointer-memaccess] memset(this, 0, sizeof(this));
--> 修改:strncpy、memset第三个参数按实际给数值。C++类构造函数初始化顺序问题:
 
  
warning: CFoobar::m_nInit will be initialized after [-Wreorder] int m_nInit; ^ warning: ‘int CFoobar::m_nTime’ [-Wreorder] int m_nTime’;
--> 修改:按声明的顺序排列。常量字符串:
 
  
warning: deprecated conversion from string constant to ‘CHAR* {aka char*}’ [-Wwrite-strings]
--> 修改:字符串要加const。3、打印格式化
 
  
warning: too few arguments for format warning: too many arguments for format
如本身要打印2个参数,但只有一个%。 myprintf("hello world %d!\n", count, getname());--> 修改:认真检查参数。4、类型一致性,类型转换整数溢出:
 
  
warning: integer overflow in expression [-Woverflow] #define BIG_SIZE 800*1024*1024*1024
转换转换:
 
  
warning: narrowing conversion of ‘height’ from ‘DWORD {aka unsigned int}’ to ‘int’ inside { } is ill-formed in C++11 [-Wnarrowing] my_dst_size foo = {width, height};
my_dst_size类型为int,但width和height为DWORD--> 修改:强制转换类型,保持一致。
 
  
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default] my_pos.pos = {0,0};
my_pos.pos为坐标。--> 修改:一一赋值。
 
  
my_pos.pos.x = 0; my_pos.pos.y = 0;
5、括号、优先级
 
  
warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses] #define HELLO ((((z>>51)^(y<<32 y="">>23)^(z<<5)))^((sum^y)+(k[p&7^e]^z)))
--> 添加括号
 
  
#define HELLO ((((z>>51)^(y<<32 y="">>23)^(z<<5)))^((sum^y)+(k[(p&7)^e]^z)))
另一个例子:
 
  
warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses] return id == foo::Get() || foo::Get() != bar::Get() && bar::Go();
6、switch
 
  
warning: enumeration value ‘LL_FOO’ not handled in switch [-Wswitch]
-->枚举类型没有完全使用,如没有default语句。添加之7、
 
  
warning: the address of ‘filename’ will always evaluate as ‘true’ [-Waddress] #define LL_STR(foo) foo ? foo : ""
其它
 
  
error: function declaration isn’t a prototype -->函数声明如无参数,要加上void
版权声明:本文为迟思堂主人李迟原创文章,如转载请注明出处,并附带原文超链接地址。用于商业用途请付稿费¥100/千字。谢谢。 https://blog.csdn.net/subfate/article/details/45168987
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值