- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
gcc 4.8 在构建时给我一个错误
#include <string.h>
#include <stdio.h>
static inline void toto(char str[3])
{
snprintf(str, sizeof(str), "XX");
}
int main(){
char str[3];
toto(str);
return 0;
}
这是gcc错误
错误:“snprintf”调用中“sizeof”的参数与目标表达式相同;您是要提供明确的长度吗?
注意:我使用 -Wall -Werror 标志将警告转换为错误。
There is something similar here在评论中,有人回答了这个问题
“对于固定长度的缓冲区,我通常使用 strncpy(dest, src, sizeof(dest)); dest[sizeof(dest)-1] = '\0'; 这保证了 NULL 终止并且只是更少比 snprintf 更麻烦,更不用说很多人使用 snprintf(dest, sizeof(dest), src); 代替,并且当他们的程序任意崩溃时感到非常惊讶。”
但这是错误的:gcc 4.8 说
“错误:‘strncpy’调用中‘sizeof’的参数与目标表达式相同;您是要提供明确的长度吗?[-Werror=sizeof-pointer-memaccess]”
in gcc 4.8 documentation, they are talking about this issue :他们说:
-Wall 的行为已更改,现在包含新的警告标志 -Wsizeof-pointer-memaccess。这可能会导致在使用以前版本的 GCC 干净编译的代码中出现新警告。
例如,
include string.h
struct A { };
int main(void)
{
A obj;
A* p1 = &obj;
A p2[10];
memset(p1, 0, sizeof(p1)); // error
memset(p1, 0, sizeof(*p1)); // ok, dereferenced
memset(p2, 0, sizeof(p2)); // ok, array
return 0;
}
提供以下诊断:警告:“void memset(void*, int, size_t)”调用中“sizeof”的参数与目标表达式相同;你的意思是取消引用它吗? [-Wsizeof-pointer-memaccess] memset(p1, 0, sizeof(p1));//错误 ^尽管这些警告不会导致编译失败,但通常 -Wall 与 -Werror 结合使用,结果,新的警告变成了新的错误。要修复,请重新编写以使用 memcpy 或取消引用有问题的 memset 调用中的最后一个参数。*
好吧,在他们的例子中,很明显代码是错误的,但在我的例子中,使用 snprintf/strncpy,我不明白为什么,我认为这是 gcc 的误报错误。对吧?
谢谢你的帮助
最佳答案
当您将数组传递给函数时,它会衰减为指向第一个元素的指针。所以你有什么
static inline void toto(char str[3]) {..}
不是数组而是指针。
因此,gcc 正确地警告。
无论您是否在函数参数中指定大小都没有关系,因为:
static inline void toto(char str[3])
和
static inline void toto(char str[])
和
static inline void toto(char *str)
都是等价的。
关于c - snprintf 错误。 sizeof 的参数与目标相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19202368/
在我从事的项目(一个 Windows 和 Ubuntu 应用程序)中,我定义了一个与日志库交互的宏。本质上,这个宏接受类似 printf 的输入,转换为字符串,并将其发送到日志库。我附上一个使用 st
我正在尝试使用 snprintf基于我读过的手册的函数是 的一部分header 但是我收到一个错误,它已被隐式声明。这是我的代码: #include #include #include str
当我通过 Fortify 分析器工具之一运行我的代码时,它提示 snprintf 说“格式字符串参数没有正确限制函数可以写入的数据量” 我了解 snprintf 不应导致缓冲区溢出。但仍然是为什么该工
在我的代码中,我使用 snprintf 如下所示,并且能够看到以下行为 char text[30] = {0}; snprintf(text, sizeof(text), "%s", "hello")
我创建了以下程序: #include #include #define TESTER "name=%s " int main(){ char *x; x = malloc(100);
当我通过 Fortify 分析器工具之一运行我的代码时,它提示 snprintf 说“格式字符串参数没有正确限制函数可以写入的数据量” 我了解 snprintf 不应导致缓冲区溢出。但仍然是为什么该工
我收到 format not a string literal and no format arguments当我在 Linux 上编译它时发出警告。 snprintf显示 const char*对于
char symbols[16] = ""; int index = 0; while (1) { if (index % 2) snprintf(symbols, siz
我正在尝试了解 snprintf 并找到了 this answer举个例子: char buf[20] = ""; char *cur = buf, * const end = buf + sizeo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试理解 C 中的 char 指针。基本上我正在做的是声明一个 char* 指针 main 并在另一个函数中通过引用传递并在那里进行修改。现在,我想打印 main 中 char 的值,这给了我段
我正在使用 C 语言(指针、指针-指针等,我喜欢它)做我的第一步,所以如果这是一个愚蠢的问题,请仁慈。 该片段不输出任何内容: char buf[256]; snprintf(buf, sizeof
我需要将一些数字(整数和 double )转换为 std::strings,并且出于性能原因不能使用 stringstream(我们发现它在同时使用时非常慢) 如果能做这样的事情就好了 templat
我已经查看这段代码一段时间了,但似乎找不到我的错误。我想我知道 snprintf(dest, n, fmt, src) 应该如何工作(它在填充 后返回 src 缓冲区中剩余的字符数>dest 缓冲区与
为什么 snprintf 在第二种情况下给出不同的值。是不是因为任何整数限制。您能否解释一下 snprintf 的工作原理以及为什么会出现负值 #include #include main() {
考虑以下代码: char *myContent = "content"; int size = snprintf(NULL, 0, "INSERT INTO myTable (col1) VALUES
我正在编写自己的 ncurses 库,突然发现在 GDB 中 snprintf() 返回的长度大于我指定的长度。这是定义的行为还是我的一些错误? (可重现的)片段代码是这样的: niko: snipp
我写了一个简单的小程序来使用 snprintf 读取具有以下格式的文件, skip first 15 chars , next 9 chars are sequence number, next 2
我使用snprintf 将格式化的数据写入磁盘,但我有一个问题,如何将它保存到用户的主目录? snprintf(filename, sizeof(filename), "%s_%s.sho", cl
定义: CHAR_BACKSLASH 定义为 '\\' 或 0x5C 变量: workingDir 是一个 C 字符串 myFilePath 是一个 C 字符串 int len = strlen(wo
我是一名优秀的程序员,十分优秀!