undefined-behavior

GCC 4.8 with GNU STL produces bad code for std::string constructor?

喜欢而已 提交于 2019-12-10 12:32:52
问题 So a bit of C++ code: void func( const std::string& theString ) { std::string theString( theString ); theString += " more string"; std::cout << theString; } which compiles fine with GCC 4.8 and VS 2013 . From my C++ knowledge, the code is okay with a local variable theString being brought into scope which then hides theString from the function argument. At the point of theString construction, the only theString in scope is the function argument which is passed to the std::string constructor.

How does clang manage to compile this code with undefined behavior into this machine code?

元气小坏坏 提交于 2019-12-10 11:32:01
问题 It's a variation of code from this tweet, just shorter one and not causing any damage to noobs. We have this code: typedef int (*Function)(); static Function DoSmth; static int Return7() { return 7; } void NeverCalled() { DoSmth = Return7; } int main() { return DoSmth(); } You see that NeverCalled() is never called in the code, don't you? Here's what Compiler Explorer shows when clang 3.8 is selected with -Os -std=c++11 -Wall Code emitted is: NeverCalled(): retq main: movl $7, %eax retq as if

Interfaces in C

戏子无情 提交于 2019-12-10 11:19:06
问题 I'm designing an application and came across an implementation issue. I have the following struct definition: app.h : struct application_t{ void (*run_application)(struct application_t*); void (*stop_application)(struct application_t*); } struct application_t* create(); The problem came when I tried to "implement" this application_t . I tend to define another struct: app.c : struct tcp_application_impl_t{ void (*run_application)(struct application_t*); void (*stop_application)(struct

iOS App will not die, writes to console & plays sounds after quit

☆樱花仙子☆ 提交于 2019-12-10 10:54:43
问题 My app has some kind of Zombie problem. (Not an NSZombie problem. Like, a coming-back-from-the-dead problem.) I first noticed that after a debugging session, when I would go for a run the music on my iPhone would pause every ~7 minutes, and when I'd unlock the device the app name would be flashing red in the status bar as though it had just crashed. Sometimes there would even be phantom sound from the app, like it was still running in the background somehow. Manually quit the app, continue.

Reading double to platform endianness with union and bit shift, is it safe?

末鹿安然 提交于 2019-12-10 10:37:44
问题 All the examples I've seen of reading a double of known endianness from a buffer to the platform endianness involve detecting the current platform's endianess and performing byte-swapping when necessary. On the other hand, I've seen another way of doing the same thing except for integers that uses bit shifting (one such example). This got me thinking that it might be possible to use a union and the bitshift technique to read doubles (and floats) from buffers, and a quick test implementation

confusion about short data type format specifier in C

一个人想着一个人 提交于 2019-12-10 10:06:52
问题 Consider following program: #include <stdio.h> int main() { short a=9; //printf("%hi\n",a); printf("%d",a); // LINE 6 } According to this the format specifier for short type (signed) is %hi Is the short type variable always gets promoted automatically to int before performing any operation on it? Is it undefined behavior , If I use %d format specifier to print the value of variable in this program? I compiled it using gcc -Wall -Wextra -WFormat options but still compiler isn't showing any

What is the definition of bitwise operators in C++?

自闭症网瘾萝莉.ら 提交于 2019-12-10 09:39:36
问题 According to the standard, operator << yields an Undefined Behavior for a negative signed first operand. C++11 5.8.2 The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero- filled. If E1 has an unsigned type, the value of the result is E1 × 2 pow E2, reduced modulo one more than the maximum value representable in the result type. Otherwise, if E1 has a signed type and non-negative value, and E1 × 2 pow E2 is representable in the result type, then that is the

Dereference null is not always UB?

独自空忆成欢 提交于 2019-12-10 03:38:37
问题 I've always known for a fact that the Standard mandates dereferencing null is UB. However, (Link 1) says p = 0; *p; is not inherently an error. and provides a link to (Link 2) says *p is not an error when p is null unless the lvalue is converted to an lvalue (I believe it's a typo and probably should read lvalue is converted to an rvalue ) Link 1 also says char* p = 0; char *q = &*(p) is "not undefined", which I could only read as well-defined or at least implementation-defined Can a language

Comma operator and void expression

半城伤御伤魂 提交于 2019-12-10 02:58:50
问题 I came across this code snippet 1 int return_printChar1() { // code // oops! no return statement } int return_printChar2() { // code return printf("Return"); } int main() { int i; // some more code i = return_printChar2(); if((return_printChar1(),i)) { printf ("Gotcha"); } } 1: This is not a real life example. My question is " Is the behaviour of the code snippet well defined in C and C++? " My take: In C the behaviour is well defined because 6.5.17 says The left operand of a comma operator

Does accessing an int with a char * potentially have undefined behavior?

旧巷老猫 提交于 2019-12-10 01:58:53
问题 The code below for testing endianness is expected to have implementation defined behavior: int is_little_endian(void) { int x = 1; char *p = (char*)&x; return *p == 1; } But is it possible that it may have undefined behavior on purposely contrived architectures? For example could the first byte of the representation of an int with value 1 (or another well chosen value) be a trap value for the char type? As noted in comments, the type unsigned char would not have this issue as it cannot have