undefined-behavior

Can unsigned integer incrementation lead to undefined defined behavior?

倾然丶 夕夏残阳落幕 提交于 2020-01-03 16:00:49
问题 After reading the 32 bit unsigned multiply on 64 bit causing undefined behavior? question here on StackOverflow, I began to ponder whether typical arithmetic operations on small unsigned types could lead to undefined behavior according to the C99 standard. For example, take the following code: #include <limits.h> ... unsigned char x = UCHAR_MAX; unsigned char y = x + 1; The x variable is initialized to the maximum magnitude for the unsigned char data type. The next line is the issue: the

Output of the program changes with printf() statement

半城伤御伤魂 提交于 2020-01-02 12:22:50
问题 Below is a C program to find the equilibrium point in the given array. #include <stdio.h> void equilibrium(int a[], int n) { int i; int rsum[n], lsum[n]; lsum[0] = a[0]; rsum[0] = a[n-1]; for (i = 1 ; i < n ; i++) { lsum[i] = lsum[i-1]+a[i]; } printf("lsum array: "); for (i = 0 ; i < n ; i++) { printf("%d ",lsum[i]); } printf("\n\nrsum array: "); for (i = n - 1 ; i >= 0 ; i--) { rsum[i] = rsum[i + 1] + a[i]; } for (i = 0 ; i < n ; i++) { printf("%d ", rsum[i]); } printf("\n\n"); for (i = 1 ;

Could reinterpret_cast turn an invalid pointer value into a valid one?

情到浓时终转凉″ 提交于 2020-01-02 07:14:16
问题 Consider this union: union A{ int a; struct{ int b; } c; }; c and a are not layout-compatibles types so it is not possible to read the value of b through a : A x; x.c.b=10; x.a+x.a; //undefined behaviour (UB) Trial 1 For the case below I think that since C++17, I also get an undefined behavior: A x; x.a=10; auto p = &x.a; //(1) x.c.b=12; //(2) *p+*p; //(3) UB Let's consider [basic.type]/3: Every value of pointer type is one of the following: a pointer to an object or function (the pointer is

Calling C function which takes no parameters with parameters

时间秒杀一切 提交于 2020-01-02 06:54:10
问题 I have some weird question about probably undefined behavior between C calling convention and 64/32 bits compilation. First here is my code: int f() { return 0; } int main() { int x = 42; return f(x); } As you can see I am calling f with an argument while f takes no parameters. My first question was does this argument is really given to f while calling it. The mysterious lines After a little objdump I obtained curious results. While passing x as argument of f: 00000000004004b6 <f>: 4004b6: 55

Can unsafe type punning be fixed by marking a variable volatile?

泪湿孤枕 提交于 2020-01-02 05:28:11
问题 In zwol's answer to Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member? he gives an example of why a simple typecast between similar structs isn't safe, and in the comments there is a sample environment in which it behaves unexpectedly: compiling the following with gcc on -O2 causes it to print "x=1.000000 some=2.000000" #include <stddef.h> #include <stdio.h> #include <stdlib.h> struct base { double some; char

Multiple compound assignments in a single statement: is it Undefined Behavior or not?

二次信任 提交于 2020-01-02 01:17:17
问题 I can't find a definitive answer for this: does the following code have undefined behavior? int x = 2; x+=x+=x+=2.5; 回答1: The behavior is undefined. Let's look at the slightly simpler expression: x += (x+=1) In C++11, the value computation of the left x is unsequenced relative to the value computation of the expression (x+=1) . This means that value computation of x is unsequenced relative to the assignment to x (due to x+=1 ), and therefore the behavior is undefined. The reason for this is

Does C have an equivalent of std::less from C++?

不想你离开。 提交于 2020-01-01 23:13:33
问题 I was recently answering a question on the undefined behaviour of doing p < q in C when p and q are pointers into different objects/arrays. That got me thinking: C++ has the same (undefined) behaviour of < in this case, but also offers the standard library template std::less which is guaranteed to return the same thing as < when the pointers can be compared, and return some consistent ordering when they cannot. Does C offer something with similar functionality which would allow safely

Does undefined behavior really help modern compilers to optimize generated code?

拜拜、爱过 提交于 2020-01-01 17:27:07
问题 Aren't modern compilers smart enough to be able to generate a code that is fast and safe at the same time? Look at the code below: std::vector<int> a(100); for (int i = 0; i < 50; i++) { a.at(i) = i; } ... It's obvious that the out of range error will never happen here, and a smart compiler can generate the next code: std::vector<int> a(100); for (int i = 0; i < 50; i++) { a[i] = i; } // operator[] doesn't check for out of range ... Now let's check this code: std::vector<int> a(unknown

Is (++i)++ undefined behavior?

寵の児 提交于 2020-01-01 09:42:36
问题 Is (++i)++ undefined behavior? Is it possible that the side effect of prefix increment happens after retrieving the incremented object for postfix increment to operate on? That would seem strange to me. My gut feeling says this is undefined in C++03 and well-defined in C++11. Am I right? 回答1: My gut feeling says this is undefined in C++03 and well-defined in C++0x. Yes you are right. The behaviour is undefined in C++03 because you are trying to modify i more than once between two sequence

Ramifications of C++20 requiring two's complement

╄→尐↘猪︶ㄣ 提交于 2020-01-01 08:35:15
问题 C++20 will specify that signed integral types must use two's complement. This doesn't seem like a big change given that (virtually?) every implementation currently uses two's complement. But I was wondering if this change might shift some "undefined behaviors" to be "implementation defined" or even "defined." Consider, the absolute value function, std::abs(int) and some of its overloads. The C++ standard includes this function by reference to the C standard, which says that the behavior is