Forum C# No pointers allowed?

No pointers allowed?

Postby fatezero » April 6th, 2010, 2:17 am

Okay, so I'm new to Player.IO and I've been doing a lot of messing around. Thus far everything has been working beautifully; as an aside I must say that it's a very well designed system. My only problem so far is the inability to pass pointers (or references as C# calls them; I've been using the term pointer for over thirteen years, I'm not about to change the lingo ;D) to a function. It compiles fine, but when I actually go to run the test server it throws an error. Something similar to "MyGame.GameCode.TestFunc(...) has argument/parameter of the non-allowed type: System.Int32&".

I'm a newcomer to C# so it's quite possible that I've screwed something up. But this reads to me as if the Player.IO system itself is disallowing the use of pointers. It seems a bit silly to me, if that's true. Unless there's a massive difference between pointers and the C# reference that I'm not aware of, you'll be taking a huge performance hit over time.

So, anyways, what's the dealio? Am I being stupid? Or are we, on the server-side, completely screwed on the use of pointers?
fatezero
 
Posts: 3
Joined: March 31st, 2010, 11:02 pm

Re: No pointers allowed?

Postby Oliver » April 7th, 2010, 9:23 am

Hi,

Thanks for the kind words about Player.IO, we really appreciate it.

You're half-right, and half-wrong :-)

The right part:
The error message you're seeing is in fact Player.IO telling you that we don't allow you to use explicit pointer manipulation in C#. That includes unsafe code as well. The reason is that we need to ensure that all code will behave in a predictable manner when it's running on our cluster; which is also why you don't have access to the full .NET library of classes (only the widely used safe subset) and why you're not allowed static variables (because you're code might be running on multiple different machines).

The wrong part:
Everything except value-types are always passed by reference in c#[1], so it's not true that we disallow passing-by-reference.

The only place where you'll run into issues is if you use the "out" and "ref" keywords to share access to value-type variables between different methods.

The "out" and "ref" keywords are very rarely used in C#, so this has not been an issue at all so far.

My advice would simply be to wrap the value types in a class, and simply pass a reference to that, if you really need to edit the same value from different methods.

If you're not changing the values across different methods, simply don't use the ref and out keywords. As [1] will tell you, you're not actually gaining any performance from using these. Copying a value will take the same amount of time as copying a pointer :-)

- Oliver

[1] Actually, that's not the whole truth: Everything is really passed as a copy, It's just that copying a pointer (which is what a variable pointing to an object really is) will give you another pointer to the same object, thus giving the effect of passing-by-reference.
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: No pointers allowed?

Postby fatezero » April 7th, 2010, 11:10 pm

Oh alright. That makes perfect sense, then.

Thanks for taking the time to clear this up, :D.
fatezero
 
Posts: 3
Joined: March 31st, 2010, 11:02 pm


Return to C#



cron