undefined-behavior

Weird results for conditional operator with GCC and bool pointers

删除回忆录丶 提交于 2020-06-27 06:42:11
问题 In the following code, I memset() a stdbool.h bool variable to value 123 . (Perhaps this is undefined behaviour?) Then I pass a pointer to this variable to a victim function, which tries to protect against unexpected values using a conditional operation. However, GCC for some reason seems to remove the conditional operation altogether. #include <stdio.h> #include <stdbool.h> #include <string.h> void victim(bool* foo) { int bar = *foo ? 1 : 0; printf("%d\n", bar); } int main() { bool x; bool

In practice, why would different compilers compute different values of int x = ++i + ++i;?

与世无争的帅哥 提交于 2020-06-23 22:30:54
问题 Consider this code: int i = 1; int x = ++i + ++i; We have some guesses for what a compiler might do for this code, assuming it compiles. both ++i return 2 , resulting in x=4 . one ++i returns 2 and the other returns 3 , resulting in x=5 . both ++i return 3 , resulting in x=6 . To me, the second seems most likely. One of the two ++ operators is executed with i = 1 , the i is incremented, and the result 2 is returned. Then the second ++ operator is executed with i = 2 , the i is incremented,

In practice, why would different compilers compute different values of int x = ++i + ++i;?

偶尔善良 提交于 2020-06-23 22:28:30
问题 Consider this code: int i = 1; int x = ++i + ++i; We have some guesses for what a compiler might do for this code, assuming it compiles. both ++i return 2 , resulting in x=4 . one ++i returns 2 and the other returns 3 , resulting in x=5 . both ++i return 3 , resulting in x=6 . To me, the second seems most likely. One of the two ++ operators is executed with i = 1 , the i is incremented, and the result 2 is returned. Then the second ++ operator is executed with i = 2 , the i is incremented,

Is it undefined behavior to “trash” a return value by casting the function pointer to a void function then calling it?

一曲冷凌霜 提交于 2020-06-17 00:09:43
问题 Say, something like this: int SayHelloThenReturnTen(void) { puts("Hello world"); return 10; } Then later: ((void(*)(void))SayHelloThenReturnTen)(); Is that safe? Is it safe to "trash" the return value by casting to a void pointer? Is this safe and cross platform, or is this undefined behavior ? 回答1: Is that safe? It is safe to cast a function pointer to any function pointer type. Casting the result back to the original type will yield a pointer value equal to the the original pointer. It is

Pointer dereference array index

旧街凉风 提交于 2020-06-01 07:36:45
问题 Having this: #include <stdio.h> #include <stdlib.h> struct Test { char c; } foo; int main (void) { struct Test **ar; ar=malloc(16); *(ar+1) = &foo; ar[1]->c = 'c'; //this work (*(*ar+1)).c = 'c'; //this does't work return 0; } //(**(ar+1)).c='c'; --> first case Why the above works only the variant with array entry and not pointer dereference? struct Test { char c; } foo; int main (void) { struct Test **ar; ar=malloc(16); *ar=malloc(0); *(ar+1) = &foo; //(**(ar+1)).c='c'; (*(*ar+1)).c='c'; //

why does long long 2147483647 + 1 = -2147483648? [duplicate]

情到浓时终转凉″ 提交于 2020-05-20 15:45:42
问题 This question already has answers here : C++ literal integer type (2 answers) Closed 12 days ago . Why doesn't this code print same number? : long long a, b; a = 2147483647 + 1; b = 2147483648; printf("%lld\n", a); printf("%lld\n", b); I know that int variable's maximum number is 2147483647 because int variable is 4 byte. But as I know, long long variable is 8 byte, but why does that code act like that? 回答1: 2147483647 + 1 is executed as the sum of two ints and therefore overflows. 2147483648

Is missing a required include undefined behavior?

六月ゝ 毕业季﹏ 提交于 2020-05-13 05:29:31
问题 As I wrote an answer to How is it possible to use pow without including cmath library I fear to have proven that missing an include of a needed header is actually undefined behavior, but since I have not found any consent of that fact I like to impose the formal question: Is missing a required header i.e. #include <iostream> int main() { std::cout << std::pow(10, 2); } Ill-formed ( [defns.ill.formed] ) code? Invoking undefined behavior ( [defns.undefined] )? If it is not 1 and 2, is it

Is missing a required include undefined behavior?

给你一囗甜甜゛ 提交于 2020-05-13 05:29:10
问题 As I wrote an answer to How is it possible to use pow without including cmath library I fear to have proven that missing an include of a needed header is actually undefined behavior, but since I have not found any consent of that fact I like to impose the formal question: Is missing a required header i.e. #include <iostream> int main() { std::cout << std::pow(10, 2); } Ill-formed ( [defns.ill.formed] ) code? Invoking undefined behavior ( [defns.undefined] )? If it is not 1 and 2, is it

Weird behaviour with class fields when adding to a std::vector

浪尽此生 提交于 2020-05-09 20:06:41
问题 I have found some very weird behaviour (on clang and GCC) in the following situation. I have a vector, nodes , with one element, an instance of class Node . I then call a function on nodes[0] that adds a new Node to the vector. When the new Node is added, the calling object's fields are reset! However, they seem to return to normal again once the function has finished. I believe this is a minimal reproducible example: #include <iostream> #include <vector> using namespace std; struct Node;

Should I declare my function template specializations or is defining them enough?

孤者浪人 提交于 2020-05-09 07:34:08
问题 I have some classes which can be checked. The code which implements this declares a function template in a header file and specializes it in different source files: // check.h template <class T> bool check(const T& object); // class1.h struct Class1 {int mass;}; // check_class1.cpp #include "class1.h" #include "check.h" template <> bool check(const Class1& object) {return object.mass < 100;} // class2.h struct Class2 {int price;}; // check_class2.cpp #include "class2.h" #include "check.h"