Skip to main content

Questions tagged [undefined-behavior]

Filter by
Sorted by
Tagged with
-1 votes
5 answers
2k views

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 ...
Anon's user avatar
  • 3,623
1 vote
1 answer
337 views

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 ...
user avatar
59 votes
11 answers
14k views

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 ...
Sisir's user avatar
  • 858
-1 votes
1 answer
255 views

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, ...
Josh's user avatar
  • 175
3 votes
5 answers
2k views

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, ...
JDługosz's user avatar
  • 598
1 vote
2 answers
1k views

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 ...
moonman239's user avatar
  • 2,063
2 votes
2 answers
286 views

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 ...
Vikaton's user avatar
  • 255
10 votes
5 answers
2k views

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 ...
supercat's user avatar
  • 8,609
1 vote
3 answers
2k views

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 ...
user avatar
4 votes
0 answers
295 views

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 ...
supercat's user avatar
  • 8,609
9 votes
5 answers
2k views

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 ...
thb's user avatar
  • 747
18 votes
5 answers
8k views

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 ...
Eight's user avatar
  • 325
5 votes
2 answers
393 views

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 ...
SHOUBHIK BOSE's user avatar
63 votes
13 answers
10k views

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 ...
Alok Save's user avatar
  • 1,138
15 votes
10 answers
5k views

What makes C developers so curious if "i++ == ++i"? [closed]

Just a random observation, it seems that on StackOverflow.com, there are questions about if "++i == i++". That question gets asked all the time though, I think I saw it asked about 6 or 7 times in the ...
Michael Stum's user avatar
  • 1,778