如何为丢失位的隐式转换生成警告? [英] how can i generate warnings for implicit casts that lose bits?
问题描述
这是我推出的一个帖子(使用Google网上论坛)被删除了
google:
i我正在使用gcc:
$ gcc -v
使用内置规格。
目标:i386-redhat-linux
配置:。 ./configure --prefix = / usr --mandir = / usr / share / man -
infodir = / usr / share / info --enable-shared --enable-threads = posix - -
enable-checking = release --with-system-zlib --enable -__ cxa_atexit -
disable-libunwind-exceptions --enable-libgcj-multifile - enable-
languages = c,c ++,objc,obj-c ++,java,fortran,ada --enable-java-awt = gtk -
disable-dssi - -with-java-home = / usr / lib / jvm / java-1.4.2-gcj-1.4.2.0 / jre
--with-cpu = generic --host = i386-redhat- linux
线程模型:posix
gcc版本4.1.1 20060525(Red Hat 4.1.1-1)
并且有编译了一个简单的测试程序(FILE:hello.c):
//
// $ gcc -Wconversion -o hello hello.c >
// $ hello
//
#include< stdio.h>
main()
{
unsigned long a_ulong = 0; // 32位
短a_short_array [128]; //每个16位
a_ulong = 1234567;
a_short_array [26] = a_ulong;
printf("%d,%hx,%x,%lx \ n",sizeof(a_short_array),
a_short_array [26],a_short_array [26],a_ulong);
//
// printf输出为:
//
// 256,d687,ffffd687,12d687 < br $>
//
}
并按原样运行:
$ gcc -Wconversion -o hello hello.c
$ hello
获得输出:
256,d687,ffffd687 ,12d687
现在,我已经确认短路是16位而无符号长是
32位。为什么这行代码没有:
a_short_array [26] = a_ulong;
在我设置-Wconversion或-Wall标志时生成警告
gcc调用行?
显然有丢失的位数(或更改值)。
这里是手册中说的是什么:
来自 http://gcc.gnu.org/onlinedocs/gcc/Wa...arning-Options
:
-Wconversion
对可能改变值的隐式转换发出警告。这个
包括实数和整数之间的转换,比如abs(x)当x是
double时;有符号和无符号之间的转换,如unsigned ui =
-1;和转换为较小的类型,如sqrtf(M_PI)。对于像abs((int)x)和ui =(unsigned)-1这样的显式强制转换,不要警告
,或者如果
,则转换后的值不会改变在abs(2.0)。警告
关于有符号和无符号整数之间的转换可以被禁用
使用-Wno-sign-conversion。
对于C ++ ,还警告NULL和非指针
类型之间的转换;混淆用户定义转换的重载决策;并且
转换永远不会使用类型转换运算符:
转换为void,相同类型,基类或对
的引用他们。有关有符号和无符号整数之间转换的警告
默认情况下在C ++中被禁用,除非-Wsign-conversion明确地启用了
。
我需要点击其他一些编译器标志吗?我不知道为什么这个
没有产生警告。
最后,请回复两个新闻组,因为我不会闲逛
comp.lang.c非常多。
谢谢你,
r bj
here is a post i put out (using Google Groups) that got dropped by
google:
i am using gcc as so:
$ gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --
disable-dssi --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20060525 (Red Hat 4.1.1-1)
and have compiled a simple test program (FILE: hello.c):
//
// $ gcc -Wconversion -o hello hello.c
// $ hello
//
#include <stdio.h>
main()
{
unsigned long a_ulong = 0; // 32 bit
short a_short_array[128]; // 16 bit each
a_ulong = 1234567;
a_short_array[26] = a_ulong;
printf("%d, %hx, %x, %lx \n", sizeof(a_short_array),
a_short_array[26], a_short_array[26], a_ulong );
//
// printf output is:
//
// 256, d687, ffffd687, 12d687
//
}
and ran it as so:
$ gcc -Wconversion -o hello hello.c
$ hello
getting output:
256, d687, ffffd687, 12d687
now, i have confirmed that a short is 16 bits and an unsigned long is
32 bits. why does not this line of code:
a_short_array[26] = a_ulong;
generate a warning when i have the -Wconversion or -Wall flags set on
the gcc invocation line?
there is clearly a loss of bits (or a changing of value).
here is what the manual says about it:
from http://gcc.gnu.org/onlinedocs/gcc/Wa...arning-Options
:
-Wconversion
Warn for implicit conversions that may alter a value. This
includes conversions between real and integer, like abs (x) when x is
double; conversions between signed and unsigned, like unsigned ui =
-1; and conversions to smaller types, like sqrtf (M_PI). Do not warn
for explicit casts like abs ((int) x) and ui = (unsigned) -1, or if
the value is not changed by the conversion like in abs (2.0). Warnings
about conversions between signed and unsigned integers can be disabled
by using -Wno-sign-conversion.
For C++, also warn for conversions between NULL and non-pointer
types; confusing overload resolution for user-defined conversions; and
conversions that will never use a type conversion operator:
conversions to void, the same type, a base class or a reference to
them. Warnings about conversions between signed and unsigned integers
are disabled by default in C++ unless -Wsign-conversion is explicitly
enabled.
is there some other compiler flag i need to hit? i don''t get why this
doesn''t generate a warning.
finally, please reply to both newsgroups as i don''t hang around
comp.lang.c very much.
thank you,
r b-j
推荐答案
gcc -v
使用内置规格。
目标:i386-redhat-linux
配置:../ configure --prefix = / usr --mandir = / usr / share / man -
infodir = / usr / share / info --enable -shared --enable-threads = posix -
enable-checking = release --with-system-zlib --enable -__ cxa_atexit -
disable-libunwind-例外--enable-libgcj-multifile --enable-
languages = c,c ++,objc,obj-c ++,java,fortran,ada --enable-java-awt = gtk -
disable-dssi --with-java-home = / usr / lib / jvm / java-1.4.2-gcj-1.4.2.0 / jre
--with-cpu = generic --host = i386-redhat-linux
线程模型:posix
gcc版本4.1.1 20060525(Red Hat 4.1.1-1)
并编译了一个简单的测试程序(FILE:hello.c):
//
//
gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --
disable-dssi --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20060525 (Red Hat 4.1.1-1)
and have compiled a simple test program (FILE: hello.c):
//
//
gcc -Wconversion -o hello hello.c
//
gcc -Wconversion -o hello hello.c
//
你好
/ /
#include< stdio.h>
main()
{
unsigned long a_ulong = 0; // 32位
短a_short_array [128]; //每个16位
a_ulong = 1234567;
a_short_array [26] = a_ulong;
printf("%d,%hx,%x,%lx \ n",sizeof(a_short_array),
a_short_array [26],a_short_array [26],a_ulong);
//
// printf输出为:
//
// 256,d687,ffffd687,12d687 < br $>
//
}
并按原样运行:
hello
//
#include <stdio.h>
main()
{
unsigned long a_ulong = 0; // 32 bit
short a_short_array[128]; // 16 bit each
a_ulong = 1234567;
a_short_array[26] = a_ulong;
printf("%d, %hx, %x, %lx \n", sizeof(a_short_array),
a_short_array[26], a_short_array[26], a_ulong );
//
// printf output is:
//
// 256, d687, ffffd687, 12d687
//
}
and ran it as so:
这篇关于如何为丢失位的隐式转换生成警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!