fixed (void** ppInputBlock = &pTwinCatInputs)中写法在c#里啥用?

strife013 2011-08-12 08:37:41
fixed (void** ppInputBlock = &pTwinCatInputs)
...全文
69 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
csproj 2011-08-12
  • 打赏
  • 举报
回复
google “c# unsafe”
showjim 2011-08-12
  • 打赏
  • 举报
回复
pTwinCatInputs应该在初始化的之前fixed
        unsafe static void voidPoint(void* pTwinCatInputs)
{
void** ppInputBlock = &pTwinCatInputs;
}
杨友山 2011-08-12
  • 打赏
  • 举报
回复
我也没见过这种写法啊
小心眼 2011-08-12
  • 打赏
  • 举报
回复
fixed 语句设置指向托管变量的指针并在 statement 执行期间“钉住”该变量。如果没有 fixed 语句,则指向可移动托管变量的指针的作用很小,因为垃圾回收可能不可预知地重定位变量。C# 编译器只允许在 fixed 语句中分配指向托管变量的指针。

// statements_fixed.cs
// compile with: /unsafe
using System;

class Point
{
public int x, y;
}

class FixedTest
{
// Unsafe method: takes a pointer to an int.
unsafe static void SquarePtrParam (int* p)
{
*p *= *p;
}

unsafe static void Main()
{
Point pt = new Point();
pt.x = 5;
pt.y = 6;
// Pin pt in place:

// assume class Point { public int x, y; }
// pt is a managed variable, subject to garbage collection.
//Point pt = new Point();
// Using fixed allows the address of pt members to be taken, and "pins" pt so it isn't relocated.

fixed (int* p = &pt.x)
{
SquarePtrParam (p);
}
// pt now unpinned
//执行完语句中的代码后,任何固定变量都被解除固定并受垃圾回收的制约。因此,不要指向 fixed 语句之外的那些变量。
Console.WriteLine ("{0} {1}", pt.x, pt.y);
}
}




你最好再看下
固定变量和可移动变量

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧