当我试图比较两个UIColors的RGB组件时,我收到了这个警告
在.h文件中,我声明了这一点
-(int) ColorDiff:(UIColor *) color1 :(UIColor *)color2;
Run Code Online (Sandbox Code Playgroud)
在.m文件中
- (int) ColorDiff:(UIColor *) color1 :(UIColor *)color2{
... //get RGB components from color1& color2
// compute differences of red, green, and blue values
CGFloat red = red1 - red2;
CGFloat green = green1 - green2;
CGFloat blue = blue1 - blue2;
// return sum of squared differences
return (abs(red) + abs(green) + abs(blue));
}
Run Code Online (Sandbox Code Playgroud)
然后在同一个.m文件中,我比较了2个这样的UIColors
int d= ColorDiff(C1,C2);// I got the warning right here.
Run Code Online (Sandbox Code Playgroud)
我做了研究,人们说我必须包含头文件.我这样做但在我的情况下没有帮助.为什么我收到此错误?
我正在学习Pthreads.我的代码按照我想要的方式执行,我可以使用它.但它给了我一个关于编译的警告.
我编译使用:
gcc test.c -o test -pthread
Run Code Online (Sandbox Code Playgroud)
与GCC 4.8.1.我得到了警告
test.c: In function ‘main’:
test.c:39:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pthread_create(&(tid[i]), &attr, runner, (void *) i);
^
test.c: In function ‘runner’:
test.c:54:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
int threadnumber = (int) param;
^
Run Code Online (Sandbox Code Playgroud)
此错误来自以下代码:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_THREADS 10
int sum; /* this data is shared by the thread(s) */
void *runner(void * param);
int main(int argc, …Run Code Online (Sandbox Code Playgroud) 在我正在进行的软件项目中,我们使用了某些第三方库,遗憾的是,这些库产生了恼人的gcc警告.我们正在努力清除所有警告代码,并希望在GCC中启用treat-warnings-as-errors(-Werror)标志.有没有办法让这些我们无法修复的第三方生成的警告消失?
我有如下定义的查找表,我正在使用GCC.当我编译时,我收到警告
warning: braces around scalar initializer
Run Code Online (Sandbox Code Playgroud)
这个警告意味着什么?我该如何初始化这个LUT?我在初始化这个结构时犯了错误吗?
救命!!
typedef struct TECH
{
float velocity1, velocity2;
float temp;
float measure;
int id;
float storage[64];
}TECH;
struct TECH lut_model_1[2] = {{{296.001465},
{74.216972},
{2.025908},
{1.516384},
{1},
{0.001746,
0.000256, 0.006216, 0.005249, -0.001668, -0.001377, 0.009865, 0.010454, -0.000288, -0.005853, 0.010584, 0.015440, 0.000465, -0.000602, 0.004330, 0.005700, 0.017120,
0.233015, 0.034154, 0.244022, 0.007644, 0.385683, 0.042960, 0.406633, -0.007811, 0.346931, 0.040123, 0.387361, 0.007030, 0.225309, 0.017897, 0.241024, 0.003700,
0.103601, 0.060748, 0.121059, -0.045041, 0.076974, 0.070647, 0.148810, -0.022399, 0.074007, 0.054797, 0.141794, 0.010376, 0.052482, 0.045013, …Run Code Online (Sandbox Code Playgroud) 下面的代码通过一些位攻击执行快速反平方根操作.该算法可能是由Silicon Graphics在1990年代早期开发的,它也出现在Quake 3中. 更多信息
但是我从GCC C++编译器收到以下警告:解除引用类型惩罚指针将破坏严格别名规则
我应该使用static_cast,reinterpret_cast还是dynamic_cast在这种情况下使用?
float InverseSquareRoot(float x)
{
float xhalf = 0.5f*x;
int32_t i = *(int32_t*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}
Run Code Online (Sandbox Code Playgroud) c++ strict-aliasing gcc-warning undefined-behavior type-punning
我正在使用C99下GCC.
我有一个static inline在头文件中声明的函数,我无法修改.
该函数永不返回但未标记__attribute__((noreturn)).
如何以告诉编译器不会返回的方式调用该函数?
我从我自己的noreturn函数调用它,部分想要抑制"noreturn函数返回"警告,但也想帮助优化器等.
我已尝试在属性中包含声明,但会收到有关重复声明的警告.
我已经尝试创建一个函数指针并将该属性应用于该函数,但它表示函数属性不能应用于指向函数.
请考虑以下代码:
void foo(bool parameter) {
std::cout << parameter << "\n";
}
int main() {
foo("const char *argument");
}
Run Code Online (Sandbox Code Playgroud)
我希望编译器在传递时发出警告const char*而不是bool作为参数来发挥作用foo.
但GCC暗中转换它.我想-Wall,-Wextra和-Wpedantic,但这些问题的警告.是否有可以捕获这种隐式转换的标志(无效的参数类型)?
忽略这个函数有一个类型参数的事实,bool有些人可能会认为这是一种糟糕的代码风格.我无法重构那部分.
该标准刚刚提到这种隐式转换将会发生:
可以将整数,浮点,无范围枚举,指针和指针到成员类型的prvalue转换为bool类型的prvalue.
我知道这种行为在if (ptr)语句中非常方便,但对于我来说,在传递参数的情况下,它显然是错误的并且是错误的来源.
对于一些长的错误,gcc输出是密集的并且有很多换行等.特别是当错误很微妙时,我可能需要10-30秒的眯眼用眼睛来解析它.
我已经在一个开放的代码编辑器窗口中粘贴它,以获得一些基本的语法突出显示并启用regex的重新格式化.
有没有人发明过更自动化的方法?
我发现__attribute__ ((warn_unused_result))作为一种鼓励开发人员不要忽略函数返回的错误代码的方法非常有用,但是我需要将它与MSVC以及gcc和gcc兼容的编译器(如ICC)一起使用.Microsoft Visual Studio C/C++编译器是否具有等效机制?(到目前为止,我已经尝试过没有运气的MSDN.)
gcc似乎没有使用以下代码生成警告.如何让它产生警告?
typedef enum
{
REG8_A,
REG8_B,
REG8_C
}REG8;
typedef enum
{
REG16_A,
REG16_B,
REG16_C
}REG16;
void function(REG8 reg8)
{
}
int main(void)
{
function(REG16_A); // Should warn about wrong enum
}
Run Code Online (Sandbox Code Playgroud) gcc-warning ×10
gcc ×7
c++ ×5
c ×4
enums ×1
include ×1
int ×1
ios ×1
noreturn ×1
type-punning ×1
visual-c++ ×1