undefined-behavior

Does *&++i cause undefined behaviour in C++03?

£可爱£侵袭症+ 提交于 2020-01-01 07:31:14
问题 In another answer it was stated that prior to C++11, where i is an int , then use of the expression: *&++i caused undefined behaviour. Is this true? On the other answer there was a little discussion in comments but it seems unconvincing. 回答1: It makes little sense to ask whether *&++i in itself has UB. The deferencing doesn't necessarily access the stored value (prior or new) of i , as you can see by using this as an initializer expression for a reference. Only if an rvalue conversion is

Is it undefined behavior to exceed translation limits and are there checker tools to find it?

不羁的心 提交于 2020-01-01 04:35:06
问题 ORIGINAL QUESTION: I'm searching the C90 standard for things to be aware of, when writing hignly portable code, while having low trust in the good will of the compiler vendor, and assuming that my software might kill somebody sometimes, if I do things wrong. Let's say I'm a little paranoid. At the moment I am thinking about the "Translation limits" (5.2.4.1 ANSI/ISO 9899:1990). As pointed out in the standard and in: "Does ansi C place a limit on the number of external variables in a program?"

Is it well-defined behaviour to exit the program before main?

笑着哭i 提交于 2020-01-01 04:30:12
问题 It's definitely possible to execute code before main is called, as seen by many examples in this question. However, what if in that pre-main code, the program is told to exit via std::exit or std::abort ? Since main is defined as the start of a program, what consequences are there from exiting before the start? Upon printing something in each section, I get the following results: Format: Section : output Main : main Init (called before main) : init Exit (set up with std::atexit inside Init) :

What do Clang and GCC do when `delete`ing base classes with non-virtual destructors?

我们两清 提交于 2019-12-31 01:50:09
问题 There is already a question asking about the "real-world" behavior of delete ing a pointer to a base class that lacks a virtual destructor, but the question is restricted to a very limited case (the derived class has no members with non-trivial destructors), and the accepted answer just says there's no way to know without checking the behavior of every compiler. ....but that isn't actually very helpful; knowing that every compiler might behave differently doesn't tell us anything about the

Is printf()'s string width safe with unterminated strings?

谁说我不能喝 提交于 2019-12-30 22:59:13
问题 Is the following well defined? const char not_a_c_string[] = { 'h', 'e', 'l', 'l', 'o' }; printf( "%.5s", (const char*) not_a_c_string ); This is a question about the specific form "%.5s" , and not an how to print a possibly not NUL-terminated string? as this question has already been answered here where the "%.*s" construct is suggested. 回答1: First of all, I believe, you meant to ask about the precision , not the field width . So, your example is to look like printf( "%.5s", (const char*)

Is there a way to distinguish a GUID from just a random number?

浪子不回头ぞ 提交于 2019-12-30 09:20:06
问题 Being able to distinguish a GUID from random data can be useful when debugging obscure code defects. On Windows each GUID generated is of version 4 therefore it has '4' as the first half-byte of the third part. So if the 16-byte sequence violtates that rule it is not a version 4 GUID. For example, 567E1ECB-EA1C-42D3-A3ED-87A5D824D167 could be either a version 4 GUID or anything else, but 567E1ECB-EA1C-02D3-A3ED-87A5D824D167 //third section starts with 0, not with 4 is not a version 4 GUID.

Sequence point from function call?

。_饼干妹妹 提交于 2019-12-30 08:42:02
问题 This is yet another sequence-point question, but a rather simple one: #include <stdio.h> void f(int p, int) { printf("p: %d\n", p); } int g(int* p) { *p = 42; return 0; } int main() { int p = 0; f(p, g(&p)); return 0; } Is this undefined behaviour? Or does the call to g(&p) act as a sequence point? 回答1: No. It doesn't invoke undefined behavior. It is just unspecified , as the order in which the function arguments are evaluated is unspecified in the Standard. So the output could be 0 or 42

How does pointer comparison work in C? Is it ok to compare pointers that don't point to the same array?

倾然丶 夕夏残阳落幕 提交于 2019-12-30 08:05:42
问题 In K&R (The C Programming Language 2nd Edition) chapter 5 I read the following: First, pointers may be compared under certain circumstances. If p and q point to members of the same array, then relations like == , != , < , >= , etc. work properly. Which seems to imply that only pointers pointing to the same array can be compared. However when I tried this code char t = 't'; char *pt = &t; char x = 'x'; char *px = &x; printf("%d\n", pt > px); 1 is printed to the screen. First of all, I thought

a = (a + b) - (b = a); C++ vs php

社会主义新天地 提交于 2019-12-30 06:49:28
问题 I've been looking around and found formula: a = (a + b) - (b = a) it is supposed to swap two variables (or objects in some cases). However I tested it with C++ and php, these gave me different result. php: $a = 10; $b = 20; $a = ($a + $b) - ($b = $a); echo $a, " ", $b; This prints 20 10 C++ int a = 10; int b = 20; a = (a + b) - (b = a); std::cout << a << " " << b; This prints 10 10 Code looks the same but outputs are different, I've been thinking about two reasons: C++ code is compiling and

Does integer overflow cause undefined behavior because of memory corruption?

梦想的初衷 提交于 2019-12-30 00:13:06
问题 I recently read that signed integer overflow in C and C++ causes undefined behavior: If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. I am currently trying to understand the reason of the undefined behavior here. I thought undefined behavior occurs here because the integer starts manipulating the memory around itself when it gets too big to fit the underlying type. So I