为什么 GCC 对空指针算法的接受不被认为是一个错误? [英] Why isn't GCC's acceptance of void-pointer arithmetic considered a bug?

查看:84
本文介绍了为什么 GCC 对空指针算法的接受不被认为是一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 C 中如何禁止 void 指针运算,至少有三个不同的帖子;gcc 4.8.2 允许它,假设 void 是字节大小;以及如何打开额外的迂腐警告来触发错误.下面是一个例子:

There are at least three different posts about how void pointer arithmetic is prohibited in C; that gcc 4.8.2 allows it, assuming that a void is of byte size; and how one can turn on extra pedantic warnings to trigger an error. Here is an example:

#include <stdio.h>

/* compile gcc -Wall -o try try.c */

int main() {
  char *str="string";
  void *vp= (void *) str;

  ++vp; /* arithmetic on void point.  huh? */

  printf("%s\n", (char*)vp);
  return 0;
}

我的问题是关于在出现无效代码的情况下考虑 C 编译器应该做什么.当编译器不对无效代码发出编译错误时,这不认为是错误吗?

My question is about thinking about what a C compiler is supposed to do in case of invalid code. Is it not considered a bug when a compiler does not issue a compile error on invalid code?

无论如何,这对于编译器来说似乎是一种奇怪的行为——即使 gcc 没有发出编译错误,至少,它可能会发出带有默认编译器标志的已弃用"警告.而且,即使有-Wall,它仍然没有给出警告.嗯?这让我感到惊讶,因为 gcc 在其他方面似乎非常成熟,而且 C 并不完全是一种新颖或复杂的语言.

And this seems like bizarre behavior for a compiler, anyway — even if gcc does not issue a compile error, at the very least, it could issue a "deprecated" warning with the default compiler flags. And, even with -Wall, it is still not even giving a warning. Huh? It surprised me because gcc seems very mature otherwise and C is not exactly a novel or complex language.

推荐答案

C 标准尝试在 void* 上执行指针算术约束冲突,这意味着任何符合标准的 C 编译器必须为包含此类尝试的任何程序发出至少一个诊断消息.警告可能是非致命错误;在这种情况下,编译器可能会继续生成其行为由实现定义的代码.

The C standard makes an attempt to perform pointer arithmetic on void* a constraint violation, which means that any conforming C compiler must issue at least one diagnostic message for any program containing such an attempt. The warning may be a non-fatal error; in that case, the compiler may then go on to generate code whose behavior is defined by the implementation.

gcc,默认情况下,不会警告 void* 上的指针运算.这意味着 gcc 在默认情况下不是一个符合标准的 C 编译器.

gcc, by default, does not warn about pointer arithmetic on void*. This means that gcc, by default, is not a conforming C compiler.

有人可能会争辩说这是一个错误,但在其默认模式下,gcc 不是标准 C 的编译器,而是 GNU C.(Fortran 编译器未能成为符合 C 的编译器也不是错误.)

One could argue that this is a bug, but in its default mode gcc is not a compiler for standard C but for GNU C. (A Fortran compiler's failure to be a conforming C compiler is also not a bug.)

仔细选择的命令行选项可以强制 gcc 至少尝试符合标准.例如:

Carefully chosen command-line options can force gcc to at least attempt to be conforming. For example:

gcc -std=cXX -pedantic

其中 XX909911 之一,将导致 gcc 警告指针void* 上的算术运算.将 -pedantic 替换为 -pedantic-errors 会使其将此类算术视为致命错误.

where XX is one of 90, 99, or 11, will cause gcc to warn about pointer arithmetic on void*. Replacing -pedantic with -pedantic-errors causes it to treat such arithmetic as a fatal error.

这篇关于为什么 GCC 对空指针算法的接受不被认为是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆