undefined-behavior

Sequence points and side effects in C

大兔子大兔子 提交于 2019-12-17 07:51:06
问题 In this C-FAQ it is give about sequence point; The Standard states that; Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. In the examples i = i++; a[i] = i++; it is clear from first sentence of the statement that these examples are results in undefined behavior . In explaining second sentence of the statement it is

Sequence points and side effects in C

流过昼夜 提交于 2019-12-17 07:50:52
问题 In this C-FAQ it is give about sequence point; The Standard states that; Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. In the examples i = i++; a[i] = i++; it is clear from first sentence of the statement that these examples are results in undefined behavior . In explaining second sentence of the statement it is

Is reading an indeterminate value undefined behavior?

梦想的初衷 提交于 2019-12-17 07:47:43
问题 The question arose in the comments of an answer to the question Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int? The code in question allocates a (local) array of bool without initializing their value. const int n = 100; bool b[n]; Clearly the values in b are indeterminate. Some of the commenters opined that reading e.g. b[0] was undefined behavior. Is this stated anywhere in the C++ standard? I am still convinced of the opposite: There is clearly storage allocated

Arithmetic right shift gives bogus result?

只谈情不闲聊 提交于 2019-12-17 07:32:24
问题 I must be absolutely crazy here, but gcc 4.7.3 on my machine is giving the most absurd result. Here is the exact code that I'm testing: #include <iostream> using namespace std; int main(){ unsigned int b = 100000; cout << (b>>b) << endl; b = b >> b; cout << b << endl; b >>= b; cout << b << endl; return 0; } Now, any number that's right shifted by itself should result in 0 ( n/(2^n) == 0 with integer divide , n>1 , and positive/unsigned ), but somehow here is my output: 100000 100000 100000 Am

32 bit unsigned multiply on 64 bit causing undefined behavior?

五迷三道 提交于 2019-12-17 07:23:49
问题 So I have about this code: uint32_t s1 = 0xFFFFFFFFU; uint32_t s2 = 0xFFFFFFFFU; uint32_t v; ... v = s1 * s2; /* Only need the low 32 bits of the result */ In all the followings I assume the compiler couldn't have any preconceptions on the range of s1 or s2 , the initializers only serving for an example above. If I compiled this on a compiler with an integer size of 32 bits (such as when compiling for x86), no problem. The compiler would simply use s1 and s2 as uint32_t typed values (not

32 bit unsigned multiply on 64 bit causing undefined behavior?

本小妞迷上赌 提交于 2019-12-17 07:23:26
问题 So I have about this code: uint32_t s1 = 0xFFFFFFFFU; uint32_t s2 = 0xFFFFFFFFU; uint32_t v; ... v = s1 * s2; /* Only need the low 32 bits of the result */ In all the followings I assume the compiler couldn't have any preconceptions on the range of s1 or s2 , the initializers only serving for an example above. If I compiled this on a compiler with an integer size of 32 bits (such as when compiling for x86), no problem. The compiler would simply use s1 and s2 as uint32_t typed values (not

32 bit unsigned multiply on 64 bit causing undefined behavior?

倖福魔咒の 提交于 2019-12-17 07:23:11
问题 So I have about this code: uint32_t s1 = 0xFFFFFFFFU; uint32_t s2 = 0xFFFFFFFFU; uint32_t v; ... v = s1 * s2; /* Only need the low 32 bits of the result */ In all the followings I assume the compiler couldn't have any preconceptions on the range of s1 or s2 , the initializers only serving for an example above. If I compiled this on a compiler with an integer size of 32 bits (such as when compiling for x86), no problem. The compiler would simply use s1 and s2 as uint32_t typed values (not

In C99, is f()+g() undefined or merely unspecified?

核能气质少年 提交于 2019-12-17 07:23:09
问题 I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be called before g(), or g() before f(). I am no longer so sure. What if the compiler inlines the functions (which the compiler may decide to do even if the functions are not declared inline ) and then reorders instructions? May one get a result

Access array beyond the limit in C and C++

99封情书 提交于 2019-12-17 06:16:10
问题 int data[8]; data[9] = 1; What does the c++ standard say about it? Is this undefined behaviour? At least the C compiler (gcc -std=c99 -pedantic -W -Wall) doesn't say anything about it. Thanks. 回答1: Accessing outside the array bounds is undefined behavior, from the c99 draft standard section Annex J.2 J.2 Undefined behavior includes the follow point: An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7]

Is passing a C++ object into its own constructor legal?

强颜欢笑 提交于 2019-12-17 03:37:32
问题 I am surprised to accidentally discover that the following works: #include <iostream> int main(int argc, char** argv) { struct Foo { Foo(Foo& bar) { std::cout << &bar << std::endl; } }; Foo foo(foo); // I can't believe this works... std::cout << &foo << std::endl; // but it does... } I am passing the address of the constructed object into its own constructor. This looks like a circular definition at the source level. Do the standards really allow you to pass an object into a function before