Should I use NULL or 0?
In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.
3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
4.10 Pointer conversions [conv.ptr]
1 A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. (略...)
4.10 Pointer conversions [conv.ptr]
1 A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t.
starwing83 发表于 2012-09-26 11:41
回复 18# zylthinking
你知道ub是什么意思么?就是任何事情都可能发生,可能直接返回0让你每次运行程序随机数都一样(苹果就这样),可能程序直接崩溃(访问了非法地址神马的),也可能烧cpu……UB的意思是一切皆有可能,程序是不能依赖UB行为的,这是常识你不知道???
starwing83 发表于 2012-09-26 12:03
回复 20# zylthinking
还有,知道什么是错误么?不满需求的设计就是错误。本来一个随机迷宫,现在变成完全一致的不会变化的了,请问这都不是设计错误还有什么是错误???
starwing83 发表于 2012-09-25 21:20
回复 7# zylthinking
我刚刚在Lua邮件列表看到了一个帖子:这的确是错,浮点转整型,如果超出了整型所能表示的范围,是UB,即如果time返回一个负的浮点值(额,这个可能性似乎不大),而转换成unsigned,那么就是UB。
http://book.51cto.com/art/200803/68542.htm
未定义的行为在IBM PC中引起CPU瘫痪!
未定义的软件行为引起CPU瘫痪的说法并不像它乍听上去那样牵强。
IBM PC的显示器以显示控制芯片所提供的水平扫描速率工作。回扫变压器(flyback transformer,一种产生高电压的装置,用于加速电子以点亮显示器上的荧光物质)需要保持一个合理的频率。
然而在软件中,程序员有可能把视频芯片的扫描速率设置成零,这样就会产生一个恒定的电压输出到回归变压器的输入端。这就使它起了电阻器的作用,只是把电能转换成热能,而不是传送到屏幕。这会在数秒之内就把显示器烧毁,那就是未定义的软件行为会导致系统瘫痪的理由。
C99 6.5 Expressions
5 If an exceptional condition occurs during the evaluation of an expression
(that is, if the result is not mathematically defined or not in the range of representable values for its type),
the behavior is undefined.
C89 6.3 EXPRESSIONS
...
If an exception occurs during the evaluation of an expression
(that is, if the result is not mathematically defined or not representable),
the behavior is undefined.
...
Undefined Behavior Causes CPU Meltdown in IBM PC's!
The suggestion of undefined software behavior causing CPU meltdown isn't as farfetched as it first appears.
The original IBM PC monitor operated at a horizontal scan rate provided by the video controller chip. The flyback transformer (the gadget that produces the high voltage needed to accelerate the electrons to light up the phosphors on the monitor) relied on this being a reasonable frequency.
However, it was possible, in software, to set the video chip scan rate to zero, thus feeding a constant voltage into the primary side of the transformer. It then acted as a resistor, and dissipated its power as heat rather than transforming it up onto the screen. This burned the monitor out in seconds. Voilà: undefined software behavior causes system meltdown!
since the behavior is undefined, the compiler is free to do anything and doesn't even have to warn you about it!
6.3.1.3 Signed and unsigned integers
1 When a value with integer type is converted to another integer type other than _Bool,if
the value can be represented by the new type, it is unchanged.
2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or
subtracting one more than the maximum value that can be represented in the new type
until the value is in the range of the new type.
49)
3 Otherwise, the new type is signed and the value cannot be represented in it; either the
result is implementation-defined or an implementation-defined signal is raised.
问题在于不可能在“两秒滴答间隔之内”运行程序两次,更不可能更多次
[root@localhost test]# vi rand.c
[root@localhost test]# vi guess.sh
[root@localhost test]# chmod +x guess.sh
[root@localhost test]# ./guess.sh
number=38, you guess 1, you lost.
number=38, you guess 2, you lost.
number=38, you guess 3, you lost.
number=38, you guess 4, you lost.
number=38, you guess 5, you lost.
number=38, you guess 6, you lost.
number=38, you guess 7, you lost.
number=38, you guess 8, you lost.
number=38, you guess 9, you lost.
number=38, you guess 10, you lost.
number=38, you guess 11, you lost.
number=38, you guess 12, you lost.
number=38, you guess 13, you lost.
number=38, you guess 14, you lost.
number=38, you guess 15, you lost.
number=38, you guess 16, you lost.
number=38, you guess 17, you lost.
number=38, you guess 18, you lost.
number=38, you guess 19, you lost.
number=38, you guess 20, you lost.
number=38, you guess 21, you lost.
number=38, you guess 22, you lost.
number=38, you guess 23, you lost.
number=38, you guess 24, you lost.
number=38, you guess 25, you lost.
number=38, you guess 26, you lost.
number=38, you guess 27, you lost.
number=38, you guess 28, you lost.
number=38, you guess 29, you lost.
number=38, you guess 30, you lost.
number=38, you guess 31, you lost.
number=38, you guess 32, you lost.
number=38, you guess 33, you lost.
number=38, you guess 34, you lost.
number=38, you guess 35, you lost.
number=38, you guess 36, you lost.
number=38, you guess 37, you lost.
number=38, you guess 38, you won.
number=38, you guess 39, you lost.
number=38, you guess 40, you lost.
number=38, you guess 41, you lost.
number=38, you guess 42, you lost.
number=38, you guess 43, you lost.
number=38, you guess 44, you lost.
number=38, you guess 45, you lost.
number=38, you guess 46, you lost.
number=38, you guess 47, you lost.
number=38, you guess 48, you lost.
number=38, you guess 49, you lost.
number=38, you guess 50, you lost.
number=38, you guess 51, you lost.
number=38, you guess 52, you lost.
number=38, you guess 53, you lost.
number=38, you guess 54, you lost.
number=38, you guess 55, you lost.
number=38, you guess 56, you lost.
number=38, you guess 57, you lost.
number=38, you guess 58, you lost.
number=38, you guess 59, you lost.
number=38, you guess 60, you lost.
number=38, you guess 61, you lost.
number=38, you guess 62, you lost.
number=38, you guess 63, you lost.
number=38, you guess 64, you lost.
number=38, you guess 65, you lost.
number=38, you guess 66, you lost.
number=38, you guess 67, you lost.
number=38, you guess 68, you lost.
number=38, you guess 69, you lost.
number=38, you guess 70, you lost.
number=38, you guess 71, you lost.
number=38, you guess 72, you lost.
number=38, you guess 73, you lost.
number=38, you guess 74, you lost.
number=38, you guess 75, you lost.
number=38, you guess 76, you lost.
number=38, you guess 77, you lost.
number=78, you guess 78, you won.
number=78, you guess 79, you lost.
number=78, you guess 80, you lost.
number=78, you guess 81, you lost.
number=78, you guess 82, you lost.
number=78, you guess 83, you lost.
number=78, you guess 84, you lost.
number=78, you guess 85, you lost.
number=78, you guess 86, you lost.
number=78, you guess 87, you lost.
number=78, you guess 88, you lost.
number=78, you guess 89, you lost.
number=78, you guess 90, you lost.
number=78, you guess 91, you lost.
number=78, you guess 92, you lost.
number=78, you guess 93, you lost.
number=78, you guess 94, you lost.
number=78, you guess 95, you lost.
number=78, you guess 96, you lost.
number=78, you guess 97, you lost.
number=78, you guess 98, you lost.
number=78, you guess 99, you lost.
[root@localhost test]#
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |