The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Fixed statement and null input...

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Gunnerson

Posts: 1006
Nickname: ericgu
Registered: Aug, 2003

Eric Gunnerson is a program manager on the Visual C# team
Fixed statement and null input... Posted: Jul 13, 2004 8:43 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Gunnerson.
Original Post: Fixed statement and null input...
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium

Advertisement

I'd like some input on a change we're considering for Whidbey.

Consider the following wrapper class:

unsafe class Wrapper
{
    public void ManagedFunc(byte[] data)
    {
        fixed (byte* pData = data)
        {
            UnmanagedFunc(pData);
        }
    }     void UnmanagedFunc(byte* pData)
        } }

In this class, I've fixed a byte[] array so I can pass it to an unmanaged method. I've included “UnmanagedFunc()“ in my program to illustrate what it looks like, but I would normally be calling a C function through P/Invoke in this scenario.

The problem with this code is that some C APIs accept null values as arguments. It would be nice to be able to pass a null byte[] and have that translate to a null pointer, but the fixed statement throws if it gets a null array.

There's an obvious workaround:

 public void ManagedFuncOption1(byte[] data)
 {
   if (data == null)
  {
    UnmanagedFunc(null);
  }
  else
  {
   fixed (byte* pData = data)
     {
     UnmanagedFunc(pData);
   }
   }
 }

and a less obvious one.

public void ManagedFuncOption2(byte[] data)
{
 bool nullData = data == null;   fixed (byte* pTemp = nullData ? new byte[1] : data)
   {
   byte* pData = nullData ? null : pTemp;
   UnmanagedFunc(pData);
  }
}

Thr problem with the workarounds is that they are ugly, and they get fairly complicated if there is more than one parameter involved.

The language spec (section 18.6) says that the behavior of fixed is implementation defined if the array expression is null, so we could change our behavior so that fixing a null array would result in a null pointer rather than an exception. 

Questions:

  1. Have you written code where this would be useful?
  2. Are there situations where the change of behavior would cause you problems?
  3. Are the workarounds simple enough that this isn't an issue?

 

Read: Fixed statement and null input...

Topic: SOA, ACID, & TX - what does it all mean Previous Topic   Next Topic Topic: Direct Express Products Install

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use