Unsafe Compile Errors

L

Lint Radley

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
 
J

Jon Skeet [C# MVP]

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];

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
 
J

Jon Skeet [C# MVP]

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

fixed int test[128];

Further note: this is only available in structs, not classes.

Jon
 
L

Lint Radley

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

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];

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
 
J

Jon Skeet [C# MVP]

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.

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
 
L

Lint Radley

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
 
J

Jon Skeet [C# MVP]

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

Unlikely, to be honest.
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top