Questions tagged [undefined-behavior]
The undefined-behavior tag has no summary.
16 questions
3votes
3answers
216views
Best Practices for loading primitive data types from raw bytes
On occasion, it is very useful to reinterpret raw bytes structured data - ints, floats, etc. Eamples include reading from a mmapped file, reading some sort of in-memory data frame, or other tasks that ...
-1votes
5answers
2kviews
In C++, does dereferencing a nullptr itself cause undefined behaviour, or is it the acting upon the dereferenced pointer which is undefined?
I happen to have a reason why I might want to dereference a nullptr. Of course when I do, my program crashes, and from what I gather, this is due to the compiler playing it safe and stopping my ...
1vote
1answer
345views
Finding undefined behaviour in C for embedded system
I just started at a small company and there is a hot-needle-built software for micro controllers written in C which we are now starting to clean up to make it maintainable again. Of course this goes ...
60votes
11answers
15kviews
Why does C++ have 'undefined behaviour' (UB) and other languages like C# or Java don't?
This Stack Overflow post lists a fairly comprehensive list of situations where the C/C++ language specification declares as to be 'undefined behaviour'. However, I want to understand why other modern ...
-1votes
1answer
256views
Adding new parameter to Javascript function
What do I pass in for the existing arguments that have no value, undefined or null? Existing function foo(a, b) is called like foo(1) and foo(23, "hi"). After adding a new parameter, it's foo(a, b, ...
3votes
5answers
2kviews
On a modern compiler, how do I intentionally code for 2s complement with wraparound?
I want to compare sequence numbers (given to this code from elsewhere) that may wrap around. Simply comparing two such values will not handle the case like 0x00000002 being greater than 0xfffffffd, ...
1vote
2answers
1kviews
Why doesn't the compiler complain when I try to access a non-existent array value?
My C++ book says that if I have int anArray[25]; and I try to evaluate anArray[25] = 0; the program will simply overwrite whatever value is stored in the next memory address past the end of the ...
2votes
2answers
290views
Consistency of Undefined behavior
If there's a bug that triggers undefined behavior in a piece of code, is the undefined behavior consistent each time running it? and changes each time compiling it? For example if you had some C code ...
10votes
5answers
2kviews
When did Undefined Behavior in C jump the causality barrier
Some hyper-modern C compilers will infer that if a program will invoke Undefined Behavior when given certain inputs, such inputs will never be received. Consequently, any code which would be ...
1vote
3answers
2kviews
How much can you detect undefined behaviour using testing [closed]
I hope this question fits this site. You may know you can't detect undefined behaviour in C using compilers - and some tools (static analysis) can help you detect it. My question is more empirical - I ...
4votes
0answers
296views
Have any C compilers ever *defined* `unsigned short x=-3; x*=x;` to yield anything other than 9
In a C implementation where int can hold all values of type unsigned short, standard integer promotion rules dictate that given the code: unsigned short x=(USHORT_MAX+1)-3; // Or just -3 x*=x; a ...
9votes
5answers
2kviews
Undefined behavior, in principle
Whether in C or C++, I think that this illegal program, whose behavior according to the C or C++ standard is undefined, is interesting: #include <stdio.h> int foo() { int a; const int ...
18votes
5answers
8kviews
Undefined behaviour in Java
I was reading this question on SO which discusses some common undefined behavior in C++, and I wondered: does Java also have undefined behaviour? If that is the case, then what are some common causes ...
5votes
2answers
400views
Undefined behaviours in C
Recently I came across a number of undefined features in C, one of them being the following: https://stackoverflow.com/questions/8698048/behaviour-of-non-const-int-pointer-on-a-const-int Could ...
63votes
13answers
10kviews
Philosophy behind Undefined Behavior
C\C++ specifications leave out a large number of behaviors open for compilers to implement in their own way. There are a number of questions that always keep getting asked here about the same and we ...