pointer bug

  • Thread starter Thread starter agro_r
  • Start date Start date
A

agro_r

I think I've found a bug on Microsoft Visual C# or maybe on the .NET
Framework itself... Look at this code:

using System;

public class Bar
{
public static void Main()
{
int[] a = {5, 4, 3, 2, 1};
unsafe
{
fixed(int* ptr = a)
{
int* reader = ptr;
while(true)
{
Console.Write("@ 0xX{0:X}is ", (uint)reader);
Console.WriteLine("{0}", *(reader--));
}
}
}
}
}

This code didn't throw an exception (which I expected), but the
program just terminates... Curiously, if we use "int a" instead of
"int[] a" and modify the rest of the program accordingly, the program
will indeed throw an exception. Does anyone know how I can contant
the .NET/C# team about this problem?
 
agro_r said:
I think I've found a bug on Microsoft Visual C# or maybe on the .NET
Framework itself... Look at this code:

using System;

public class Bar
{
public static void Main()
{
int[] a = {5, 4, 3, 2, 1};
unsafe
{
fixed(int* ptr = a)
{
int* reader = ptr;
while(true)
{
Console.Write("@ 0xX{0:X}is ", (uint)reader);
Console.WriteLine("{0}", *(reader--));
}
}
}
}
}

unsafe code isn't guaranteed to throw an exception when you do bad things.
That's why it's unsafe.

Once you dereference reader-1, all bets are off.

David
 
I assumed that when we play with unsafe code and do something really
bad, an exception will be thrown.

I still find it inconsistent that on some occurence it throws an
exception, and on some occurence it just quits.
 
Back
Top