Error while using stackalloc in C#

Hi,

I am working on a project in which i am writing unsafe code in few public methods in a public class. i declared the method as unsafe and added "using System.Runtime.InteropServices;" namespace as well but when i am building my solution i an getting this error:

1. Invalid expression term 'stackalloc'

2. ; expected

3. Invalid expression term 'byte'

4. ; expected

5. Invalid expression term ']'

The line where i am getting this error is:

char* tmpPtr = (char*)stackallocbyte[(2 * 0x99)];

Any help or advice to rectify the same is highly appreciated.

Thanks

Yogesh

[1003 byte] By [YogeshC] at [2008-1-8]
# 1

Hi Yogesh, I moved your post to the Visual C# Language forum, where folks should be able to help you.

Cheers,

JJustice [MSFT]

JohnJustice-MSFT at 2007-10-2 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2
Hi Yogesh.
You cannot use stackalloc as a part of an expression (e.g. cast expression). It can only be an initializer of a local variable.

For instance,

char* p = stackalloc char[100];

is allowed, but

Foo(stackalloc char[100]);

or

char* p;
p = stackalloc char[100];

or

char* p = (stackalloc char[100]);

or

class A { char* p = stackalloc char[100]; }

are not allowed.

nikov at 2007-10-2 > top of Msdn Tech,Visual C#,Visual C# Language...