unspecified-behavior

In C99, is f()+g() undefined or merely unspecified?

女生的网名这么多〃 提交于 2019-11-27 04:09:48
I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be called before g(), or g() before f(). I am no longer so sure. What if the compiler inlines the functions (which the compiler may decide to do even if the functions are not declared inline ) and then reorders instructions? May one get a result different of the above two? In other words, is this undefined behavior? This is not because I intend to write

Why does a main function without a return statement return value 12?

本秂侑毒 提交于 2019-11-26 22:23:34
问题 I have written a program that prints a table. I have not included the return syntax in the main function, but still whenever I type echo $? it displays 12. My source code : #include <stdio.h> int main(void) { int ans,i,n; printf("enter the no. : "); scanf("%d",&n); for(i=1;i<=10;i++) { ans = n*i; printf("%d * %d = %d\n",n,i,ans); } } I have not written return 12, but still it returns 12 every time I execute the program. Thanks. 回答1: As swegi says, it's undefined behavior. As Steve Jessop et

Is pointer comparison undefined or unspecified behavior in C++?

筅森魡賤 提交于 2019-11-26 21:38:18
问题 The C++ Programming Language 3rd edition by Stroustrup says that, Subtraction of pointers is defined only when both pointers point to elements of the same array (although the language has no fast way of ensuring that is the case). When subtracting one pointer from another, the result is the number of array elements between the two pointers (an integer). One can add an integer to a pointer or subtract an integer from a pointer; in both cases, the result is a pointer value. If that value does

How to implement memmove in standard C without an intermediate copy?

前提是你 提交于 2019-11-26 19:56:09
From the man page on my system: void *memmove(void *dst, const void *src, size_t len); DESCRIPTION The memmove() function copies len bytes from string src to string dst. The two strings may overlap ; the copy is always done in a non-destructive manner. From the C99 standard: 6.5.8.5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, theycompare equal. If the objects pointed to are

Does this code from “The C++ Programming Language” 4th edition section 36.3.6 have well-defined behavior?

自作多情 提交于 2019-11-26 15:58:31
In Bjarne Stroustrup's The C++ Programming Language 4th edition section 36.3.6 STL-like Operations the following code is used as an example of chaining : void f2() { std::string s = "but I have heard it works even if you don't believe in it" ; s.replace(0, 4, "" ).replace( s.find( "even" ), 4, "only" ) .replace( s.find( " don't" ), 6, "" ); assert( s == "I have heard it works only if you believe in it" ) ; } The assert fails in gcc ( see it live ) and Visual Studio ( see it live ), but it does not fail when using Clang ( see it live ). Why am I getting different results? Are any of these

Is it unspecified behavior to compare pointers to different arrays for equality?

雨燕双飞 提交于 2019-11-26 11:23:18
问题 The equality operators have the semantic restrictions of relational operators on pointers: The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators except for their lower precedence and truth-value result. [C++03 §5.10p2] And the relational operators have a restriction on comparing pointers: If two pointers p and q of the same type point to different objects that are not members of the same object or

Unspecified, undefined and implementation defined behavior WIKI for C

南楼画角 提交于 2019-11-26 11:08:20
问题 Although there is plentiful of links about this subject on SO, I think that there is something missing: a clear explanation in plain language of what are the differences between unspecified behavior (UsB), undefined behaviour (UB) and implementation-defined behavior (IDB) with detailed but easy explanation of any use-case and example. Note: I made the UsB acronym up for sake of compactness in this WIKI, but don\'t expect to see it used elsewhere. I know this may seem a duplicate of other

How to implement memmove in standard C without an intermediate copy?

夙愿已清 提交于 2019-11-26 06:34:10
问题 From the man page on my system: void *memmove(void *dst, const void *src, size_t len); DESCRIPTION The memmove() function copies len bytes from string src to string dst. The two strings may overlap ; the copy is always done in a non-destructive manner. From the C99 standard: 6.5.8.5 When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both

Undefined, unspecified and implementation-defined behavior

空扰寡人 提交于 2019-11-26 01:17:10
问题 What is undefined behavior in C and C++? What about unspecified behavior and implementation-defined behavior? What is the difference between them? 回答1: Undefined behavior is one of those aspects of the C and C++ language that can be surprising to programmers coming from other languages (other languages try to hide it better). Basically, it is possible to write C++ programs that do not behave in a predictable way, even though many C++ compilers will not report any errors in the program! Let's