fixed

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code in one of my programs;

ert= CO3;
int Code = SR(ert, 1024, 1024);

char[] UnsafeTempBuffer = new char[128];
fixed ( char* tBuffer= UnsafeTempBuffer)
{
Code = Siofunc(tBuffer, 128);
}
When I try to compile I receive the error, Pointer may only be used in an
unsafe context. Isn't that what I'm doing?
So what's wrong with it? The error is called for the line with fixed in
it.
 
Dave said:
I have the following code in one of my programs;

ert= CO3;
int Code = SR(ert, 1024, 1024);

char[] UnsafeTempBuffer = new char[128];
fixed ( char* tBuffer= UnsafeTempBuffer)
{
Code = Siofunc(tBuffer, 128);
}
When I try to compile I receive the error, Pointer may only be used in an
unsafe context. Isn't that what I'm doing?
So what's wrong with it? The error is called for the line with fixed in
it.

You should tag the method that does the fixing, with the keyword 'unsafe',
as follows:

public unsafe void f()
{
// can use fixed expression
}

Or, You can make a certain scope unsafe by again using the 'unsafe' keyword,
as follows:

public void f()
{
// ... safe code
unsafe
{
// ... unsafe code here (eg fixed statements and use of pointers)
}
// ... safe code
}

Hope this helps,
 
Dave said:
I have the following code in one of my programs;

ert= CO3;
int Code = SR(ert, 1024, 1024);

char[] UnsafeTempBuffer = new char[128];
fixed ( char* tBuffer= UnsafeTempBuffer)
{
Code = Siofunc(tBuffer, 128);
}
When I try to compile I receive the error, Pointer may only be used in an
unsafe context. Isn't that what I'm doing?
So what's wrong with it? The error is called for the line with fixed in
it.

Dave,

You must also go the your project properties, and set the "Allow unsafe code
blocks" option to true.

Cheers,
 
Thanks, that solved my problem.

TT (Tom Tempelaere) said:
Dave said:
I have the following code in one of my programs;

ert= CO3;
int Code = SR(ert, 1024, 1024);

char[] UnsafeTempBuffer = new char[128];
fixed ( char* tBuffer= UnsafeTempBuffer)
{
Code = Siofunc(tBuffer, 128);
}
When I try to compile I receive the error, Pointer may only be used in an
unsafe context. Isn't that what I'm doing?
So what's wrong with it? The error is called for the line with fixed in
it.

You should tag the method that does the fixing, with the keyword 'unsafe',
as follows:

public unsafe void f()
{
// can use fixed expression
}

Or, You can make a certain scope unsafe by again using the 'unsafe' keyword,
as follows:

public void f()
{
// ... safe code
unsafe
{
// ... unsafe code here (eg fixed statements and use of pointers)
}
// ... safe code
}

Hope this helps,
 
Back
Top