标签: compiler-warnings

c array - warning:format不是字符串文字

我正在尝试学习C,但我已经遇到了问题.我认为它是微不足道的,但我需要知道它.我已经写了:

#include <stdio.h>
#include <string.h>

int main() 
{
    char str_a[20];

    strcpy(str_a, "Hello, world!\n");
    printf(str_a);
}
Run Code Online (Sandbox Code Playgroud)

一旦我尝试使用以下命令编译它:gcc -g -o char_array2 char_array2.c我收到错误说:

char_array2.c: In function ‘main’:
char_array2.c:9:2: warning: format not a string literal and no format arguments [-Wformat-security]
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

c compiler-warnings

40
推荐指数
3
解决办法
7万
查看次数

assert()与消息

我通过以下方式看到某处断言与消息一起使用:

assert(("message", condition));
Run Code Online (Sandbox Code Playgroud)

这似乎很有效,除了gcc抛出以下警告:

warning: left-hand operand of comma expression has no effect
Run Code Online (Sandbox Code Playgroud)

我怎么能停止警告?

c gcc assert compiler-warnings

39
推荐指数
3
解决办法
3万
查看次数

禁用gcc中的特定警告

在Microsoft编译器上,可以使用#pragma禁用特定警告,而不禁用其他警告.如果编译器警告"必须完成"的事情,这是一个非常有用的功能.

GCC此时是否有类似功能?这似乎是一个显而易见的功能,它无法想象它还没有这个功能,但网上的旧信息表明这个功能不存在.

在GCC中使用什么?

具体来说,我喜欢使用多字符常量,比如'abc'.这些有效地评估为基数256 - 这是一个非常方便的功能,但它会触发警告.它非常便于在case语句中切换四个字符串.

gcc compiler-warnings

37
推荐指数
3
解决办法
4万
查看次数

未经检查的ArrayAdapter调用

当我实例化我的ArrayAdapter(编译正常)时,我收到以下警告:

warning: [unchecked] unchecked call to ArrayAdapter(android.content.Context,int,java.util.List<T>) as a member of the raw type android.widget.ArrayAdapter
      ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(CFAMain.this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
Run Code Online (Sandbox Code Playgroud)

这是问题所在:

ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(CFAMain.this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
Run Code Online (Sandbox Code Playgroud)

任何人都有任何想法,为什么它给我这个警告?

java android warnings compiler-warnings

37
推荐指数
1
解决办法
1万
查看次数

警告C4003:宏'max'的实际参数不够 - Visual Studio 2010 C++

在Visual Studio 2010 SP1上编译openFrameworks 007项目时,我有以下警告:

d:\pedro\development\videoflow\openframeworks\libs\openframeworks\types\ofcolor.h(127): warning C4003: not enough actual parameters for macro 'max'
d:\pedro\development\videoflow\openframeworks\libs\openframeworks\types\ofcolor.h(128): warning C4003: not enough actual parameters for macro 'max'
d:\pedro\development\videoflow\openframeworks\libs\openframeworks\graphics\ofpixels.h(150): warning C4003: not enough actual parameters for macro 'max'
d:\pedro\development\videoflow\openframeworks\libs\openframeworks\graphics\ofpixels.h(151): warning C4003: not enough actual parameters for macro 'max'
Run Code Online (Sandbox Code Playgroud)

据我所知,这个警告通常会出现错误,但在我的情况下一切正常.受影响的代码如下:

const float srcMax = ( (sizeof(SrcType) == sizeof(float) ) ? 1.f : numeric_limits<SrcType>::max() );
const float dstMax = ( (sizeof(PixelType) == sizeof(float) ) ? 1.f : numeric_limits<PixelType>::max() );
Run Code Online (Sandbox Code Playgroud)

我尝试在预处理器上设置NOMINMAX,但由于openFrameworks还在ofConstants.h上定义了NOMINMAX,因此我收到了一些已经定义了NOMINMAX的警告.

我试图在受影响的openFrameworks文件上定义NOMINMAX,但它会产生相同的警告(事实上,如果我分析of​​Color.h和ofPixel.h中包含的文件,它们最终包括ofConstants.h,因此应该定义NOMINMAX).

关于如何解决这个问题的任何想法?如果你不......最好的是什么?这个警告或NOMINMAX已定义的一堆警告?

编辑:

BTW当我谈到错误时我正在讨论这些:警告C4003和错误C2589和C2059:x …

c++ compiler-warnings openframeworks visual-studio

37
推荐指数
2
解决办法
2万
查看次数

GCC可以警告我修改C99中const结构的字段吗?

我在尝试制作常量正确的代码时偶然发现了一个小问题.

我本来想写一个函数,它接受一个const结构的指针,告诉编译器"请告诉我,如果我正在修改结构,因为我真的不想".

我突然想到编译器会允许我这样做:

struct A
{
    char *ptrChar;
};

void f(const struct A *ptrA)
{
    ptrA->ptrChar[0] = 'A'; // NOT DESIRED!!
}
Run Code Online (Sandbox Code Playgroud)

这是可以理解的,因为实际上const是指针本身,而不是它指向的类型.我想让编译器告诉我,我正在做一些我不想做的事情,但是,如果可能的话.

我使用gcc作为我的编译器.虽然我知道上面的代码应该是合法的,但我仍然检查它是否会发出警告,但没有任何结果.我的命令行是:

gcc -std=c99 -Wall -Wextra -pedantic test.c
Run Code Online (Sandbox Code Playgroud)

有可能解决这个问题吗?

c const c99 compiler-warnings

37
推荐指数
4
解决办法
1603
查看次数

在 C 和 C++ 中比较不同大小的无符号整数时如何收到警告?

C 或 C++ 中错误的常见来源是这样的代码:

size_t n = // ...

for (unsigned int i = 0; i < n; i++) // ...
Run Code Online (Sandbox Code Playgroud)

当溢出时可以无限循环unsigned int

例如,在 Linux 上,unsigned int是 32 位,而size_t是 64 位,因此 if n = 5000000000,我们会得到一个无限循环。

我如何使用 GCC 或 Clang 获得有关此问题的警告?

GCC不这样做:-Wall -Wextra

#include <stdint.h>

void f(uint64_t n)
{
    for (uint32_t i = 0; i < n; ++i) {
    }
}
Run Code Online (Sandbox Code Playgroud)
gcc-13 -std=c17 \
       -Wall -Wextra -Wpedantic \
       -Warray-bounds -Wconversion \
       -fanalyzer \ …
Run Code Online (Sandbox Code Playgroud)

c c++ gcc clang compiler-warnings

37
推荐指数
4
解决办法
2610
查看次数

禁止"丢弃非单位值"警告

我已经将scalac命令行参数添加-Ywarn-value-discard到我的构建中,因为这会捕获我在代码中找到的一个微妙的错误.但是,我现在得到一些关于"丢弃的非单位价值"的警告,这些警告是关于有意丢弃的,而不是错误的.我该如何压制这些警告?

scala compiler-warnings

36
推荐指数
3
解决办法
1万
查看次数

多字符恒定警告

为什么这是一个警告?我认为在很多情况下使用multi-char int常量而不是"无意义"数字或者使用相同值定义const变量更为明确.解析wave/tiff /其他文件类型时,可以更清楚地将读取值与某些"EVAW","数据"等进行比较,而不是相应的值.

示例代码:

int waveHeader = 'EVAW';
Run Code Online (Sandbox Code Playgroud)

为什么这会发出警告?

c c++ portability casting compiler-warnings

35
推荐指数
5
解决办法
8万
查看次数

为什么连续抛出2个异常不会产生无法访问的代码警告?

为什么以下代码行没有创建编译器警告?

void Main()
{
  throw new Exception();
  throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)

在我看来,编译器应通知您无法达到第二次抛出异常.

c# throw compiler-warnings

34
推荐指数
2
解决办法
1031
查看次数