c 语言编译器基本错误类型,GCC编译器上布尔类型的C和C ++语言的默认初始化程序是64而不是0.这是编译器错误?(Default initializer of C and C++ language...

GCC编译器上布尔类型的C和C ++语言的默认初始化程序是64而不是0.这是编译器错误?(Default initializer of C and C++ languages of boolean types on GCC compiler is 64 instead of 0. It's compiler bug?)

我正在研究D语言并同时与C和C ++语言进行比较。它在dmd和gdc编译器中都运行良好,但是当我在gcc编译器上测试时,我发现了一个看起来像GCC编译器的bug,布尔值的默认初始化器type而不是0 / false请参阅以下代码:

C ++代码

#include

using namespace std;

int main()

{

bool b;

cout << b << endl;

return 0;

}

G ++编译器(gcc版本4.4.3(Ubuntu 4.4.3-4ubuntu5.1):

g++ -Wall -pedantic test.cpp

test.cpp: In function ‘int main()’: test.cpp:7: warning: ‘b’ is used

uninitialized in this function

./a.out 64

C代码(foo.c):

#include

#include

#define bool _Bool

int main(int argc, char * args[])

{

bool b;

printf("%d\n", b);

return 0;

}

gcc编译器

gcc-4.6 -Wall -pedantic a.c

foo.c: In function ‘main’:

foo.c:9:8: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]

./a.out

64

tcc编译器

tcc -Wall foo.c

./a.out

0

铿锵编译器

clang -Wall -pedantic foo.c

./a.out

0

有人可以解释一下gcc的行为吗?

I'm studying D language and simultaneously doing comparison to C and C++ languages.It works fine both dmd and gdc compilers,but when I tested on gcc compiler,I found a thing that looks like bug of GCC compiler,the default initializer of boolean type instead of 0/false see the following code:

C++ code

#include

using namespace std;

int main()

{

bool b;

cout << b << endl;

return 0;

}

G++ compiler(gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1):

g++ -Wall -pedantic test.cpp

test.cpp: In function ‘int main()’: test.cpp:7: warning: ‘b’ is used

uninitialized in this function

./a.out 64

C code(foo.c):

#include

#include

#define bool _Bool

int main(int argc, char * args[])

{

bool b;

printf("%d\n", b);

return 0;

}

gcc compiler

gcc-4.6 -Wall -pedantic a.c

foo.c: In function ‘main’:

foo.c:9:8: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]

./a.out

64

tcc compiler

tcc -Wall foo.c

./a.out

0

clang compiler

clang -Wall -pedantic foo.c

./a.out

0

Can someone explain the gcc behavior?

原文:https://stackoverflow.com/questions/10307948

更新时间:2019-12-12 23:59

最满意答案

C ++中基本类型的默认初始化意味着“未初始化”。 也就是说,任何价值都可以存在。 你得到64因为恰好在那个记忆位置。

如果要进行值初始化,则需要使用bool() :

bool b = bool(); //Now is false.

值初始化有效意味着将基本类型初始化为零。

C ++ 11使这个更清晰:

bool b{}; //Now is false.

Default initialization in C++ for basic types means "uninitialized". That is, any value can be there. You got 64 because that just happened to be in that memory location.

If you want to do value initialization, then you need to use bool():

bool b = bool(); //Now is false.

Value initialization effectively means initializing basic types to zero.

C++11 makes this rather cleaner:

bool b{}; //Now is false.

2012-04-25

相关问答

说明 在你的.travis.yml我们看到: - if [ "$CXX" == "clang++" ]; then export CXX="clang++" && bii cpp:build; fi

- if [ "$CXX" == "g++" ]; then export CXX="g++" && bii cpp:build; fi

biicode的命令bii cpp:build使用biicode的默认生成器运行CMake,它是UNIX平台的"Unix Makefiles" (

...

gcc的具体历史记录在GCC Wiki上提供 。 更普遍的一点是,编译器通常最初编译有一些其他编译器,直到它们足够强大以编译自己。 或者,可以编写一个基本的编译器,可以在汇编器中处理您的功能的子集,并从中构建。 但是再一次,这几乎不再需要了。 有大量的编译器可用于各种语言。 即使斯蒂芬·约翰逊(Stephen Johnson)正在编写pcc(第一个C编译器之一),也有可用的编译器B以及许多其他语言。 gcc有几个编译器最初选择构建它,而RMS表示他至少在初始开发过程中使用了Pastel编译器。 记

...

您可能没有在两次尝试之间正确清理。 做一个make distclean并再试一次。 抱歉。 You likely didn't clean up properly in-between tries. Do a make distclean and try again. Sorry.

C ++中基本类型的默认初始化意味着“未初始化”。 也就是说,任何价值都可以存在。 你得到64因为恰好在那个记忆位置。 如果要进行值初始化,则需要使用bool() : bool b = bool(); //Now is false.

值初始化有效意味着将基本类型初始化为零。 C ++ 11使这个更清晰: bool b{}; //Now is false.

Default initialization in C++ for basic types means "uninitialized". T

...

您可以先下载Code :: Blocks,它是一个可以下载的跨平台IDE,包括MinGW GCC编译器。 http://www.codeblocks.org/ 但是如果你使用的是Linux,你可以在终端中使用一个“gcc”命令。 You can start by downloading Code::Blocks, its an cross-platform IDE that can be downloaded including MinGW GCC compiler. http://www.cod

...

有两种不同的C ++运行时库实现:gcc的libstdc++和clang的libc++ ,它们彼此不兼容。 将使用从libc++更改为libstdc++ 。 There are 2 different implementation of C++ run-time library: gcc's libstdc++ and clang's libc++ which are incompatible with each other. Change the use from libc++ to libst

...

根据@duskwuff,我更喜欢使用clang++ ,但是如果你必须兼容,那么你可以通过macports安装真正的GNU编译器。 安装macports(包括xcode-select步骤)后,只需执行以下操作: $ sudo port selfupdate

$ sudo port install gcc46

(或gcc47等) 编译器将在你的$PATH (如果你正确设置了macport),但显然,它将是/opt/local/bin/gcc46 (参见sudo port select gcc )。

...

访问控制严格来说是一个编译时的概念:如果源代码中的所有访问都没问题,那么代码将会被编译。 在运行时没有什么可以检查的,因此访问限定符对生成的代码没有影响。 Access control is strictly a compile-time notion: if all of the accesses in the source code are okay, then the code compiles. There's nothing left to check at runtime, so ac

...

我很确定这是MSVC中的一个错误。 由于某种原因,编译器在decltype上窒息(因为它是语法错误,很可能它们还没有实现它)。 这是一个解决方法: using workaround = decltype(line);

using Ctx = typename workaround::context_type;

这似乎是相关的错误报告 。 它被关闭为deferred ...... :( I'm pretty sure that's a bug in MSVC. The compiler is ch

...

当你给GCC一个标准时,根据手册 编译器将接受遵循该标准的所有程序以及使用不与其相矛盾的GNU扩展的程序。 如果您想确保严格遵守标准,请添加-pedantic ,这将警告hex hex文字。 When you give a standard to the GCC, according to the manual the compiler will accept all programs following that standard and those using GNU extensions t

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值