.NET Framework - Unsafe Compile Errors

Asked By Lint Radley on 30-Aug-07 08:50 AM
Hello,

I've been attempting to experiement with unsafe code in order to speed
up performance. I've read several examples online, including a Microsoft
one

All of the examples seem declare their pointers the way I have below.
However, when I try to I get the following errors all referring to the
int* line:


public unsafe class MyClass
{

int* test = stackalloc int [100];


....
Invalid expression term 'stackalloc'
; expected
Array size cannot be specified in a variable declaration (try
initializing with a 'new' expression)
Invalid token ';' in class, struct, or interface member declaration



Allow unsafe code is turned on in the projects settings. I am using the
.NET 2.0 Framework.

Anyone have any ideas on where I am going wrong?

Thank you,

Lint Radley




Jon Skeet [C# MVP] replied on 30-Aug-07 09:30 AM
On Aug 30, 1:50 pm, Lint Radley <scanoma...@forwardbounding.com>

stackalloc is meant to be used for a *local* variable, not a *member*
variable.

You can use the "fixed" modifier in unsafe contexts in C# 2:

fixed int test[128];

What are you trying to do exactly? Do you have reason to believe that
unsafe code is actually going to help you?

Jon
Jon Skeet [C# MVP] replied on 30-Aug-07 09:32 AM
Further note: this is only available in structs, not classes.

Jon
Lint Radley replied on 30-Aug-07 09:41 AM
Dear Jon,

Thanks for the reply - that would explain it.

In the code I included, I was just tinkering with unsafe to learn how it
works. The real use would be in an image processing application I am
working on. The images are 512x512; the pixel data is currently stored
in a managed two dimensional int array. A colleague of mine suggested I
look into unsafe for performance gains in accessing the pixel data.

Thanks again,

Lint Radley
Jon Skeet [C# MVP] replied on 30-Aug-07 10:00 AM
On Aug 30, 2:41 pm, Lint Radley <scanoma...@forwardbounding.com>

I suspect you wouldn't get much benefit from that apart from when
you're transferring the data to/from the screen or file (or whatever).

As always, work out how much performance you need, write the simplest
code which works, and then tune performance if you really need to.

Jon
Lint Radley replied on 30-Aug-07 11:51 AM
Hi Jon,

Do you think there'd be much performance benefit for accessing the data
in statistical calculations? That's the big area of our software.

We've actually just started the phase, as you described, of
optimization. What took five seconds now takes approximately 1.4 -
multithreaded some tasks, removed use of Lists, etc.

Lint Radley
Jon Skeet [C# MVP] replied on 30-Aug-07 01:44 PM
Unlikely, to be honest.


Right. Now, what's your target? How long can you afford it to take?
There's no point in spending a lot of time optimising if you've already
got it as fast as you need to be.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too