我遇到了一个问题,我确信这个问题很容易解决,但我很茫然......
我有一个执行以下代码的模板:
T value = d;
if ( std::numeric_limits< T >::is_signed )
{
if ( value < 0 )
{
*this += _T( "-" );
value = -(signed)value;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,由于明显的原因,GCC正在给我一个警告(由于数据类型的范围有限,比较总是错误的当为无符号类型编译此代码时).我完全理解这背后的原因,我在numeric_limits检查中查看是否可以让编译器关闭它(它适用于MSVC).在海湾合作委员会下,我得到了警告.有没有办法(没有禁用警告,我甚至不知道你是否可以使用GCC)来修复此警告?代码永远不会被调用,我会假设优化器也会编译出来但我无法摆脱警告.
有人可以给我一个解决方案吗?
干杯!
由于返回对局部变量的引用,我刚被一个令人讨厌的未定义行为所困扰.
我们知道它是邪恶的,通常编译器打印出一个很好warning的告诉我们......好吧gcc(3.4.2)似乎并没有把检查推得太远.
std::string get_env_value(std::string const& key);
std::string const& get_phase()
{
std::string const& phase = get_env_value("PHASE"); // [1]
std::cout << "get_phase - " << phase << '\n';
return phase; // [2]
}
Run Code Online (Sandbox Code Playgroud)
这编译没有故障,但我们陷入了未定义行为的讨厌境界.
行[1]是可以的,因为标准指定应该扩展绑定到const引用的变量的生命周期以匹配const引用的生命周期.
线[2]似乎也好......
在我看来,静态分析应该能够告诉我使用"终身扩展" [1],这[2]是不安全的,但它可能会很快变得丑陋我猜...
我有一个k类型的变量const char *,以及glib中的原型函数
void g_hash_table_replace(GHashTable *hash_table,
gpointer key,
gpointer value);
Run Code Online (Sandbox Code Playgroud)
gpointer 被简单地定义为
typedef void* gpointer;
Run Code Online (Sandbox Code Playgroud)
我知道在这种情况下,实际上可以k将密钥作为密钥传入g_hash_table_replace,但是gcc给了我错误
service.c:49:3: warning: passing argument 2 of ‘g_hash_table_replace’ discards ‘const’ qualifier from pointer target type [enabled by default]
/usr/include/glib-2.0/glib/ghash.h:70:13: note: expected ‘gpointer’ but argument is of type ‘const char *’
Run Code Online (Sandbox Code Playgroud)
这是用gcc 4.6.0.对于4.5.0及更早版本,一个简单的转换为(char*)足以抑制此警告,但gcc似乎已经变得"更聪明"了.我试过了(char *)(void *)k,但它仍然知道变量是最初的const.什么是沉默不调用此警告的最佳方式strdup(3)上k?
为什么GCC 4.7在函数内实例化一个类(带指针)时会抱怨?
坏:
#include "foo.h"
int fn () {
Foo *foo;
foo->method();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp:在成员函数'int foo()'中:main.cpp:21:52:警告:'fn'可以在此函数中未初始化使用[-Wuninitialized]
好:
#include "foo.h"
Foo *foo;
int fn () {
foo->method();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
好:
#include "foo.h"
int fn () {
Foo foo;
foo.method();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在多线程c应用程序中发现了这一点.作者评论说它用于在自定义断言函数中使线程崩溃.海湾合作委员会对它很好,但是clang发出以下警告:
note: consider using __builtin_trap() or qualifying pointer with 'volatile'
Run Code Online (Sandbox Code Playgroud)
并且对于assert函数的每次使用,还会发出其中一个:
warning: indirection of non-volatile null pointer will be deleted, not trap
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?__builtin_trap特定于clang?我应该用吗?
int main()
{
int i, c;
i:
for (i = 0; i < 3; i++) {
c = i &&&& i;
printf("%d\n", c);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面编译的程序的输出gcc是
0
1
1
Run Code Online (Sandbox Code Playgroud)
如何在上述计划中评估?
拿这个简短的C文件,nulltest.c,打印出"Hey":
#include <stddef.h>
#include <stdio.h>
int main() {
char c = NULL;
c = 'e';
printf("H%cy\n", c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的理解是在C中,NULL 应该扩展为空指针常量,这将使char c = NULL指针隐式转换为整数.但是,在gcc 4.8中编译时:
$ gcc --version
gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
$ gcc -Wall -Wconversion nulltest.c
$
Run Code Online (Sandbox Code Playgroud)
我没有得到任何警告.
另一方面,gcc和clang的先前版本都警告相同的代码:
$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
$ gcc nulltest.c
nulltest.c: In function ‘main’:
nulltest.c:5: warning: initialization makes integer from pointer without a cast
Run Code Online (Sandbox Code Playgroud)
在Mac OS X 10.9上:
$ clang …Run Code Online (Sandbox Code Playgroud) 我在这一行得到零错误除法:
if (tim2_st_ovf < T2_PREK_250)
Run Code Online (Sandbox Code Playgroud)
这些值的定义如下:
volatile uint8_t tim2_st_ovf = 0;
#define T2_PREK_250 ((250 * (F_CPU / 1000)) / ((UINT8_MAX + 1) * 1024))
#define F_CPU 16000000UL
Run Code Online (Sandbox Code Playgroud)
和UINT8_MAX等于255.
我为什么要这个?我在计算器上计算了好几次,得到了~15.此外,如果我将1024更改为1023它不会显示任何错误.
我已经使用在线说明下载了GNU编译器.在编译时,我继续收到以下错误:
sh:gcc:找不到命令
我试过在线搜索,但我没有运气.有谁知道为什么会这样,或者如何修复它?
PS我保存了mingw文件夹C:\mingw,我正在使用msys.bat在线建议的编译器,位于C:\mingw\msys\1.0.我试图编译的源.c文件位于D:\cfiles\task1.
感谢您的帮助
我有以下代码:
int atoi(const char * str)
{
int ret = 0, i;
if (str) {
for (i = 0; str[i] != '\0'; i++)
if (str[i] >= '0' && str[i] <= '9')
ret = ret * 10 + str[i] - '0';
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
当试图编译它时
fug@fugtop ~/p/book> make
gcc -c -o db.o db.c -Wall -Werror -std=c99 -g -DVERSION=\"v0.4\" -Wno-
unused-variable -Wno-unused-function
gcc -c -o misc.o misc.c -Wall -Werror -std=c99 -g -DVERSION=\"v0.4\" -
Wno-unused-variable -Wno-unused-function
misc.c: In function ‘atoi’:
misc.c:55:5: error: …Run Code Online (Sandbox Code Playgroud)