interior_ptr & pin_ptr

本文介绍如何使用interior_ptr来操作托管类型的内部数据,避免编译错误,并解释了其作用于防止垃圾回收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

interior_ptr :

msdn: 点击打开链接

托管类型不能直接转换成非托管类型,只能使用interior_ptr,声明个指针,里面存放托管类型变量的引用,然后才能对其使用。

例如有一个类

ref class MyClass {
public:
	int data;
};


下面的代码,编译时出错

	MyClass ^ h_MyClass = gcnew MyClass;
	h_MyClass->data = 1;
	Console::WriteLine(h_MyClass->data);

	int* np =   &(h_MyClass->data);
	*np = 4;

错误信息:

1>clic.cpp(18): error C2440: 'initializing' : cannot convert from 'cli::interior_ptr<Type>' to 'int *'
1>          Cannot convert a managed type to an unmanaged type


应该用下面的代码修正:

	MyClass ^ h_MyClass = gcnew MyClass;
	h_MyClass->data = 1;
	Console::WriteLine(h_MyClass->data);

	interior_ptr<int> p = &(h_MyClass->data);
	*p = 2;
	Console::WriteLine(h_MyClass->data);

interior_ptr :能够防止被引用的托管对象,不被垃圾回收。

(用pin_ptr可以代替 interior_ptr ??




Copied C:\1wjzl\PROCESS/.\o2mfr3.c to libudf\src (system &quot;copy &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\fluent22.1.0\src\udf\sconstruct.udf &quot;libudf\win64\3ddp_host\SConstruct&quot; &quot;) ????? 1 ??????? (system &quot;copy &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\fluent22.1.0\src\udf\scons_test.bat &quot;libudf\win64\3ddp_host\scons_test.bat&quot; &quot;) ????? 1 ??????? (chdir &quot;libudf&quot;)(chdir &quot;win64\3ddp_host&quot;)Compiler used is &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\ntbin\clang\bin\clang-cl Linker used is &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\ntbin\clang\bin\lld-link Copy(&quot;C:\1wjzl\PROCESS\libudf\win64\3ddp_host\resolve.exe&quot;, &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent\ntbin\win64\resolve.exe&quot;) Copy(&quot;C:\1wjzl\PROCESS\libudf\win64\3ddp_host\o2mfr3.c&quot;, &quot;C:\1wjzl\PROCESS\libudf\src\o2mfr3.c&quot;) c_sources [&#39;o2mfr3.c&#39;, &#39;udf_names.c&#39;] c_sources_ [&#39;o2mfr3.c&#39;] In file included from o2mfr3.c:1: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\udf\udf.h:22: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\main\global.h:266: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\storage\var.h:10: C:\0rjaz\ansys\ANSYSI~1\v221\fluent\ntbin\clang\10\Include\10.0.18362.0\shared\basetsd.h(322,12): warning: cast to &#39;void * __ptr32&#39; from smaller integer type &#39;unsigned long&#39; [-Wint-to-void-pointer-cast] return((void * POINTER_32) (unsigned long) (ULONG_PTR) p); ^ 1 warning generated. In file included from udf_names.c:3: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\udf\udf.h:22: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\main\global.h:266: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\storage\var.h:10: C:\0rjaz\ansys\ANSYSI~1\v221\fluent\ntbin\clang\10\Include\10.0.18362.0\shared\basetsd.h(322,12): warning: cast to &#39;void * __ptr32&#39; from smaller integer type &#39;unsigned long&#39; [-Wint-to-void-pointer-cast] return((void * POINTER_32) (unsigned long) (ULONG_PTR) p); ^ 1 warning generated. lld-link: error: undefined symbol: MATERIAL_SPECIES_INDEX &gt;&gt;&gt; referenced by o2mfr3.obj:(calc_interior_o2_flux) scons: *** [libudf.dll] Error 1 (system &quot;copy &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\fluent22.1.0\src\udf\sconstruct.udf &quot;libudf\win64\3ddp_node\SConstruct&quot; &quot;) ????? 1 ??????? (system &quot;copy &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\fluent22.1.0\src\udf\scons_test.bat &quot;libudf\win64\3ddp_node\scons_test.bat&quot; &quot;) ????? 1 ??????? (chdir &quot;libudf&quot;)(chdir &quot;win64\3ddp_node&quot;)Compiler used is &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\ntbin\clang\bin\clang-cl Linker used is &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent&quot;\ntbin\clang\bin\lld-link Copy(&quot;C:\1wjzl\PROCESS\libudf\win64\3ddp_node\resolve.exe&quot;, &quot;C:\0rjaz\ansys\ANSYSI~1\v221\fluent\ntbin\win64\resolve.exe&quot;) Copy(&quot;C:\1wjzl\PROCESS\libudf\win64\3ddp_node\o2mfr3.c&quot;, &quot;C:\1wjzl\PROCESS\libudf\src\o2mfr3.c&quot;) c_sources [&#39;o2mfr3.c&#39;, &#39;udf_names.c&#39;] c_sources_ [&#39;o2mfr3.c&#39;] In file included from o2mfr3.c:1: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\udf\udf.h:22: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\main\global.h:266: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\storage\var.h:10: C:\0rjaz\ansys\ANSYSI~1\v221\fluent\ntbin\clang\10\Include\10.0.18362.0\shared\basetsd.h(322,12): warning: cast to &#39;void * __ptr32&#39; from smaller integer type &#39;unsigned long&#39; [-Wint-to-void-pointer-cast] return((void * POINTER_32) (unsigned long) (ULONG_PTR) p); ^ 1 warning generated. In file included from udf_names.c:3: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\udf\udf.h:22: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\main\global.h:266: In file included from C:\0rjaz\ansys\ANSYSI~1\v221\fluent\fluent22.1.0\src\storage\var.h:10: C:\0rjaz\ansys\ANSYSI~1\v221\fluent\ntbin\clang\10\Include\10.0.18362.0\shared\basetsd.h(322,12): warning: cast to &#39;void * __ptr32&#39; from smaller integer type &#39;unsigned long&#39; [-Wint-to-void-pointer-cast] return((void * POINTER_32) (unsigned long) (ULONG_PTR) p); ^ 1 warning generated. lld-link: error: undefined symbol: MATERIAL_SPECIES_INDEX &gt;&gt;&gt; referenced by o2mfr3.obj:(calc_interior_o2_flux) scons: *** [libudf.dll] Error 1 Done.
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值