By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,411 Members | 1,504 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,411 IT Pros & Developers. It's quick & easy.

fixed

P: n/a
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.

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
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,
---
Tom Tempelaere
Nov 16 '05 #2

P: n/a
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
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,
---
Tom Tempelaere
Nov 16 '05 #3

P: n/a
Thanks, that solved my problem.

"TT (Tom Tempelaere)" wrote:
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
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,
---
Tom Tempelaere

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.