views:

393

answers:

5

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?

+9  A: 

Looking at the Wiki, the situations in which undefined behavior happens are either not allowed or throw an exception in C#.

However in Unsafe code, undefined behavior I believe is possible, as that allows you to use pointers etc.

Edit: It looks like I'm right: http://msdn.microsoft.com/en-us/library/aa664771%28VS.71%29.aspx

Has an example of undefined behavior in c#

Aequitarum Custos
When you state "the Wiki", to what Wiki are you referring?
Jeff Yates
YES, using unsafe code can produce undefined behavior. +1
md5sum
@Jeff Yates: The "undefined behavior" link up in the question goes to Wikipedia.
R. Bemrose
@Jeff Yates: Yeah, I was looking to his wikipedia link to determine what situations cause undefined behaviors.
Aequitarum Custos
A: 

Not really in the exactly Wiki sense but I suppose the most obvious example that comes to my mind is simply writing some threaded code, but then it's like that in any language.

mfloryan
That's just nondeterministic, not undefined. You're still given a guarantee of exactly what each thread will be doing. You just don't know the exact order in which things happen, and so you might get some surprising results.
jalf
@mfloryan: unfortunately, whenever you spawn a new thread in C# you can't expect a game of NetHack to be started. Ah, good days when you could play it by running gcc...
Martinho Fernandes
A: 

In general I would say no.

Use Automatic variable before it’s initialized.

All variables must be initialized. If not an exception occurs.

Division by zero

Exception is thrown.

Indexing an array out of bounds

Exception is thrown

As Aequitarum Custos pointed out you can use unsafe code. Then again this isn't really C#, you are explicitly opting out of the C# environment.

Chuck Conway
Unsafe code *is* C#. What you're opting out of is the managed code guarantees.
Martinho Fernandes
This is *not* C# unsafe { int* px1; int* px2 = F(out px1, ref px2); Console.WriteLine("*px1 = {0}, *px2 = {1}", *px1, *px2); // undefined behavior }Yes it's written with in C#.
Chuck Conway
Sure, that's perfectly legal C#. An implementation of C# that implements the optional "unsafe" subset is still an implementation of C#.
Eric Lippert
+10  A: 

As others have mentioned, pretty much anything in the "unsafe" block can yield implementation-defined behaviour; abuse of unsafe blocks allows you to change the bytes of code that make up the runtime itself, and therefore all bets are off.

The corner case of integer division has an implementation-defined behaviour.

Throwing an exception and never catching it causes implementation-defined behaviour -- terminate the process, start a debugger, and so on.

There are a number of other situations in C# where we are forced to emit code which has implementation-determined behaviour. For example, this situation:

http://blogs.msdn.com/ericlippert/archive/2006/04/06/odious-ambiguous-overloads-part-two.aspx

However, the situations in which a safe, well-behaved C# program has implementation-defined behaviour should be quite rare.

Eric Lippert
Ouch, that overload issue is really weird. And I thought DivisionByZeroException was the expected behavior on `1/0`. Anyway, I'll +1 this tomorrow...
Martinho Fernandes
Who said anything about division by zero?
Eric Lippert
@Eric: I thought the corner case you mentioned was the usual 1/0. I searched around and I found it's actually `int.MinValue/-1`.
Martinho Fernandes
+4  A: 

According to the ECMA-334 document (p. 473):

A program that does not contain any occurrences of the unsafe modifier cannot exhibit any undefined behavior.

That promotes 'implementation-defined' to the worst case, see Eric Lippert's answer.

Henk Holterman