undefined-behavior

Temporary and expression behavior

a 夏天 提交于 2019-12-12 17:06:40
问题 Is this well defined behavior? const char* p = (std::string("Hello") + std::string("World")).c_str(); std::cout << p; I am not sure. Reasons? 回答1: No, this is undefined behavior. Both std::string temporaries and the temporary returned by operator+ only live until the end of the initialization of your const char* (end of full expression). Then they are destroyed and p points to uncertain memory. 回答2: No the behaviour is undefined because p points to deallocated storage in std::cout << p; A

Will gcc skip this check for signed integer overflow?

…衆ロ難τιáo~ 提交于 2019-12-12 15:26:41
问题 For example, given the following code: int f(int n) { if (n < 0) return 0; n = n + 100; if (n < 0) return 0; return n; } Assuming you pass in a number that is very close to integer overflow (less than 100 away), will the compiler produce code that would give you a negative return? Here is an excerpt about this issue from "The Descent to C" by Simon Tatham: "The GNU C compiler (gcc) generates code for this function which can return a negative integer, if you pass in (for example) the maximum

Within the same function, is it UB to access—through indirection—a local variable not in scope?

主宰稳场 提交于 2019-12-12 15:15:48
问题 After the second closing brace, b is accessible only through indirection through a . int main() { int *a; { int b = 42; a = &b; } printf("%d", *a); // UB? return 0; } Since b is not anymore in scope, is this UB? I know it's UB to dereference a pointer to a non-static local variable from a function that has already returned, but in this case everything is within the same function. This is UB in C++, but I'm not sure about C. 回答1: Yes, it's undefined behaviour to access any variable that has

Difference between array and pointer to string literal

邮差的信 提交于 2019-12-12 14:01:30
问题 I am new to C , so it may be a dumb question. I was writing a piece of code like bellow: char ar[]="test"; *(ar+1)='r'; this is working fine. But whenever I am doing it: char *p="test"; *(p+1)="r"; this is giving segmentation fault . Can anyone please describe why second case is giving segmentation fault? explanation from memory point of view will be appreciated. 回答1: In the second case p is pointing to a string literal and you are not allowed to modify a string literal , it is undefined

Floating point division by zero not constexpr

邮差的信 提交于 2019-12-12 11:51:56
问题 When compiling this: constexpr double x {123.0}; constexpr double y = x / 0.0; std::cout << x << " / 0 = " << y << "\n"; The compiler (gcc 4.9.2, -std=c++11 or c++14) fails, giving error: (1.23e+2 / 0.0)' is not a constant expression constexpr double y = x / 0.0; How is the result (Inf) relevant when deciding if y can be a constexpr or not? For reference, this seems to be the way to do it: static constexpr double z = std::numeric_limits<double>::quiet_NaN(); static constexpr double w = std:

Is SSE2 signed integer overflow undefined?

别说谁变了你拦得住时间么 提交于 2019-12-12 11:07:04
问题 Signed integer overflow is undefined in C and C++. But what about signed integer overflow within the individual fields of an __m128i ? In other words, is this behavior defined in the Intel standards? #include <inttypes.h> #include <stdio.h> #include <stdint.h> #include <emmintrin.h> union SSE2 { __m128i m_vector; uint32_t m_dwords[sizeof(__m128i) / sizeof(uint32_t)]; }; int main() { union SSE2 reg = {_mm_set_epi32(INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX)}; reg.m_vector = _mm_add_epi32(reg

Why is this simple assignment undefined behaviour?

喜夏-厌秋 提交于 2019-12-12 10:30:19
问题 I was refreshing my understanding of value-initialisation versus default-initialisation, and came across this: struct C { int x; int y; C () { } }; int main () { C c = C (); } Apparently this is UB because In the case of C(), there is a constructor that is capable of initializing the x and y members, so no initialization takes place. Attempting to copy C() to c therefore results in undefined behavior. I think I understand why, but I'm not certain. Can someone please elaborate? Does that mean

GOTO before local variable

耗尽温柔 提交于 2019-12-12 08:18:08
问题 Does the following piece of code constitute undefined behaviour, since I am jumping before the variable declaration and using it via a pointer? If so, are there differences between the standards? int main() { int *p = 0; label1: if (p) { printf("%d\n", *p); return 0; } int i = 999; p = &i; goto label1; return -1; } 回答1: There is no undefined behavior in your program. goto statement has two constraints: (c11, 6.8.6.1p1) "The identifier in a goto statement shall name a label located somewhere

Is this undefined behavior or implementation defined?

早过忘川 提交于 2019-12-12 05:27:29
问题 Is the following undefined or implementation-defined: int x = 0; printf("%d%d", ++x, x); The order of evaluating arguments is unspecified, so: if ++x is evaluated first, this prints 11 . if x is evaluated first, it prints 10 . 回答1: printf("%d%d", ++x, x); This is clearly undefined behavior in C++. (C++11, 1.9p15) "If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object,

My code may be crashing because the 'class' isn't C compatible?

十年热恋 提交于 2019-12-12 02:53:39
问题 -edit- I narrowed it down. Reproducible: Why does passing this object in C break my code? My app is not working properly after i made a change. I got a warning in msvc but not in gcc. Heres a repo of the warning. warning C4190: 'fnA' has C-linkage specified, but returns UDT 'Test' which is incompatible with C #include <type_traits> template<class T> class Test{ T t; }; typedef Test<int> A; //static_assert(std::is_pod<A>::value, "Not a POD"); //fails in msvc 2010 static_assert(sizeof(A) ==