undefined-behavior

Does this invoke undefined behaviour?

萝らか妹 提交于 2019-12-17 16:50:45
问题 Consider the following C program: #include <stdio.h> int main(){ int a =-1; unsigned b=-1; if(a==b) printf("%d %d",a,b); else printf("Unequal"); return 0; } In the line printf("%d %d",a,b); , "%d" is used to print an unsigned type. Does this invoke undefined behavior and why? 回答1: Although you are explicitly allowed to use the va_arg macro from <stdarg.h> to retrieve a parameter that was passed as an unsigned as an int (7.15.1.1/2), in the documentation for fprintf (7.19.6.1/9) which also

Dangling references and undefined behavior

一个人想着一个人 提交于 2019-12-17 16:17:43
问题 Assume a dangling reference x . Is it undefined behavior to just write &x; or even x; ? 回答1: First off, very interesting question. I would say it is undefined behaviour, assuming "dangling reference" means "referred-to object's lifetime has ended and the storage the object occupied has been reused or released." I base my reasoning on the following standard rulings: 3.8 §3: The properties ascribed to objects throughout this International Standard apply for a given object only during its

aligned_storage and strict aliasing

℡╲_俬逩灬. 提交于 2019-12-17 15:37:32
问题 I'm currently using aligned_storage to implement an 'Optional' type similar to that of boost::optional. To accomplish this I have a class member like so: typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type t_; I use placement new to create the object, however I don't store the pointer returned anywhere. Instead, I access the underlying type of the object in all my member functions like this (obviously with checks to ensure the object is valid via a boolean flag also

Assignment operator sequencing in C11 expressions

≯℡__Kan透↙ 提交于 2019-12-17 12:19:09
问题 Introduction The C11 standard (ISO/IEC 9899:2011) has introduced a new definition of side effect sequencing within an expression (see related question). The sequence point concept has been complemented with sequenced before and sequenced after relations which are now the basis for all definitions. Section 6.5 "Expressions", point 2 says: If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the

Observable behavior and undefined behavior — What happens if I don't call a destructor?

只谈情不闲聊 提交于 2019-12-17 10:58:13
问题 Note: I've seen similar questions, but none of the answers are precise enough, so I'm asking this myself. This is a very nitpicky "language-lawyer" question; I'm looking for an authoritative answer. The C++ standard says: A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not

Code with undefined behavior in C#

China☆狼群 提交于 2019-12-17 10:56:09
问题 In C++ there are a lot of ways that you can write code that compiles, but yields undefined behavior (Wikipedia). Is there something similar in C#? Can we write code in C# that compiles, but has undefined behavior? 回答1: As others have mentioned, pretty much anything in the "unsafe" block can yield implementation-defined behaviour; abuse of unsafe blocks allows you to change the bytes of code that make up the runtime itself, and therefore all bets are off. The corner case of integer division

In which versions of the C++ standard does “(i+=10)+=10” have undefined behaviour?

前提是你 提交于 2019-12-17 10:53:20
问题 In C++, does the following have undefined behaviour: int i = 0; (i+=10)+=10; There was some debate about this in the comments to my answer to What's the result of += in C and C++? The subtlety here is that the default response seems to be "yes", whereas it appears that the correct answer is "it depends on the version of the C++ standard". If it does depend on the version of the standard, please explain where it's UB and where it's not. 回答1: tl;dr : The sequence of the modifications and reads

Is it defined behavior to reference an early member from a later member expression during aggregate initialization?

风流意气都作罢 提交于 2019-12-17 10:44:30
问题 Consider the following: struct mystruct { int i; int j; }; int main(int argc, char* argv[]) { mystruct foo{45, foo.i}; std::cout << foo.i << ", " << foo.j << std::endl; return 0; } Note the use of foo.i in the aggregate-initializer list. g++ 5.2.0 outputs 45, 45 Is this well-defined behavior? Is foo.i in this aggregate-initializer always guaranteed to refer to the being-created structure's i element (and &foo.i would refer to that memory address, for example)? If I add an explicit constructor

Macros and postincrement

£可爱£侵袭症+ 提交于 2019-12-17 10:14:25
问题 Here's some more weird macro behavior I was hoping somebody could shed light on: #define MAX(a,b) (a>b?a:b) void main(void) { int a = 3, b=4; printf("%d %d %d\n",a,b,MAX(a++,b++)); } The output is 4 6 5. The value of b is incremented twice but not before MAX displays its value. Can anybody please tell me why this is happening and how does one predict such behavior? (Another instance of why macros should be avoided!) 回答1: Macros do text substitution. Your code is equivalent to: printf("%d %d

Can different GCC dialects be linked together?

自闭症网瘾萝莉.ら 提交于 2019-12-17 09:51:41
问题 I know that in principle this is probably undefined behaviour, but in the interest of dealing with a large project, here's my question about GCC: Suppose I compile one transation unit with gcc -std=c++98 , and another with -std=c++11 , using the exact same compiler installation. Is there any sort of guarantee that I can link the two object files and obtain a well-defined program? As far as I can tell, the potential problems can only come from different views of the library headers due to