undefined-behavior

Segmentation fault in strcpy

consider the program below char str[5]; strcpy(str,"Hello12345678"); printf("%s",str); When run this program gives segmentation fault. But when strcpy is replaced with following, program runs fine. strcpy(str,"Hello1234567"); So question is it should crash when trying to copy to str any other string of more than 5 char...

What's the worst example of undefined behaviour actually possible?

In questions about things not to do in C++ such as dereferencing a dangling pointer or calling delete twice for the same address I often see the statement that it's undefined behaviour and anything can happen - the program can overwrite the system disk or offend my parents. What is the worst real example of undefined behaviour consequen...

How do we explain the result of the expression (++x)+(++x)+(++x)?

x = 1 i expect the answer to be 11, but it comes out to be 12. ...

Is using const_cast for read-only access to a const object allowed?

In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i < count; i++ ) { if( array[i] == 0 ) { ++result; } } return result...

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

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 ...

Code with undefined behavior in C#

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? ...

Why should I not try to use "this" value after "delete this"?

In this paragraph of C++ FAQ usage of delete this construct is discussed. 4 restrictions are listed. Restrictions 1 to 3 look quite reasonable. But why is restriction 4 there that I "must not examine it, compare it with another pointer, compare it with NULL, print it, cast it, do anything with it"? I mean this is yet another pointer. W...

unexpected result when adding to pointer

Someone told me this bit of code prints 29. Why is that? int *a = 17; printf("%d", a+3); ...

Best/worst examples of undefined behavior in C or C++?

Possible Duplicates: Whats the worst example of undefined behaviour actually possible? What are all the common undefined behaviour that c++ programmer should know about? What are the common undefined behaviours that Java Programmers should know about There are lots of parts of the C and C++ standards that are not completely ...

How could pairing new[] with delete possibly lead to memory leak only?

First of all, using delete for anything allocated with new[] is undefined behaviour according to C++ standard. In Visual C++ 7 such pairing can lead to one of the two consequences. If the type new[]'ed has trivial constructor and destructor VC++ simply uses new instead of new[] and using delete for that block works fine - new just call...

Can a heap-allocated object be const in C++?

In C++ a stack-allocated object can be declared const: const Class object; after that trying to call a non-const method on such object is undefined behaviour: const_cast<Class*>( &object )->NonConstMethod(); //UB Can a heap-allocated object be const with the same consequences? I mean is it possible that the following: const Class*...

Indirectly calling non-const function on a const object

Given the following code: class foo; foo* instance = NULL; class foo { public: explicit foo(int j) : i(j) { instance = this; } void inc() { ++i; } private: int i; }; Is the following using defined behavior? const foo f(0); int main() { instance->inc(); } I'm asking because I'm using a cl...

Are memory leaks "undefined behavior" class problem in C++?

Turns out many innocently looking things are undefined behavior in C++. For example, once a non-null pointer has been delete'd even printing out that pointer value is undefined behavior. Now memory leaks are definitely bad. But what class situation are they - defined, undefined or what other class of behavior? ...

Limit the confusion caused by undefined-behavior?

As I understand from my reading, undefined-behavior is the result of leaving the compiler with several non-identical alternatives at compile time. However, wouldn't that mean that if one were to follow strict coding practice (like putting each assignment and each equality in a separate statement, proper debugging and commenting) then it...

Does the following code invoke UB ?

Does the following code invoke UB ? int main(){ volatile int i = 0; volatile int* p = &i; int j = ++i * *p; } ...

Can I new[], then cast the pointer, then delete[] safely with built-in types in C++?

In my code I have effectively the following: wchar_t* buffer = new wchar_t[size]; // bonus irrelevant code here delete[] reinterpret_cast<char*>( buffer ); Types in question are all built-in and so they have trivial destructors. In VC++ the code above works allright - new[] just allocates memory, then delete[] just frees it. Is it ac...

Is null terminate() handler allowed?

In VC++7 if I do the following: void myTerminate() { cout << "In myTerminate()"; abort(); } int main( int, char** ) { set_terminate( &myTerminate ); set_terminate( 0 ); terminate(); return 0; } the program behaves exactly as if abort() was called directly which is exactly what default terminate() handler does....

How to explain undefined behavior to know-it-all newbies?

There'a a handful of situations that the C++ standard attributes as undefined behavior. For example if I allocate with new[], then try to free with delete (not delete[]) that's undefined behavior - anything can happen - it might work, it might crash nastily, it might corrupt something silently and plant a timed problem. It's so problema...

Correcting XmlReader problems using ReadToDescendant and/or ReadElementContentAsObject

I'm working on a mysterious bug in the usually very good open source project Excel Data Reader. It's skipping values reading from my particular OpenXML .xlsx spreadsheet. The problem is occurring in the ReadSheetRow method (demonstration code below). The source XML is saved by Excel and contains no whitespace which is when the strange b...

If changing a const object is undefined behavior then how do constructors and destructors operate with write access?

C++ standard says that modifying an object originally declared const is undefined behavior. But then how do constructors and destructors operate? class Class { public: Class() { Change(); } ~Class() { Change(); } void Change() { data = 0; } private: int data; }; //later: const Class object; //object.Change(); - won't co...