当前位置:网站首页>C unsafe unmanaged object pointer conversion
C unsafe unmanaged object pointer conversion
2022-07-24 14:09:00 【Ink pool Ivory】
Today, it was used by the big guys unsafe Show a wave , Just a little note
How to get the unmanaged object referred to by the pointer ( Here is the structure )AsRef
And vice versa , Get the corresponding pointer with an unmanaged object AsPointer
struct A_Part
{
public int f1;
public int f2;
}
unsafe class Program
{
static T* Alloc<T>(int size = 1) where T : unmanaged
{
Console.WriteLine(Marshal.SizeOf<T>());
IntPtr begin = Marshal.AllocHGlobal(Marshal.SizeOf<T>() * size);
return (T*)begin;
}
static ref T ToRef<T>(void* ptr) => ref Unsafe.AsRef<T>(ptr);
static T* ToPointer<T>(ref T value) where T : unmanaged => (T*)Unsafe.AsPointer(ref value);
static void Free(void* begin)
{
Marshal.FreeHGlobal((IntPtr)begin);
}
static void Main(string[] args)
{
A_Part* ptr = Alloc<A_Part>();
ptr->f1 = 1;
Console.WriteLine($"original ptr f1:{ptr->f1}");
A_Part a = *ptr;
a.f1 = 2;
Console.WriteLine($"after A ptr f1:{ptr->f1}");
ref A_Part reft = ref ToRef<A_Part>(ptr);
reft.f1 = 3;
Console.WriteLine($"after ref ptr f1:{ptr->f1}");
A_Part* ref_ptr = ToPointer(ref reft);
Console.WriteLine($"ref_ptr f1:{ref_ptr->f1}");
Free(ptr);
Console.ReadKey();
}
}边栏推荐
- How to quickly wrap lines in Excel table
- R language uses the tablestack function of epidisplay package to make statistical summary tables (descriptive statistics based on the grouping of target variables, hypothesis testing, etc.), set the b
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布、使用cex.Y.axis参数指定Y轴分组标签文本的大小
- Nmap security testing tool tutorial
- Mmdrawercontroller gets the current VC for push and pop
- threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
- Unity pedestrians walk randomly without collision
- 看完这篇文章,才发现我的测试用例写的就是垃圾
- JS get object attribute value
- Sringboot-plugin-framework 实现可插拔插件服务
猜你喜欢

电赛设计报告模板及历年资源

Unity pedestrians walk randomly without collision

Nessus security testing tool tutorial

正则表达和绕过案例

RHCE first operation

How to quickly wrap lines in Excel table

IEEE Transaction期刊模板使用注意事项
![[oauth2] III. interpretation of oauth2 configuration](/img/31/90c79dbc91ee15c353ec46544c8efa.png)
[oauth2] III. interpretation of oauth2 configuration

Network security - use exchange SSRF vulnerabilities in combination with NTLM trunking for penetration testing

About the flicker problem caused by using universalimageloader to load pictures and refresh data in recyclerview
随机推荐
Remove the treasure box app with the green logo that cannot be deleted from iPhone
Csp2021 T3 palindrome
Ztree tree Metro style mouse through the display user-defined controls add, edit, delete, down, up operations
The solution to the error of [installation detects that the primary IP address of the system is the address assigned by DHCP] when installing Oracle10g under win7
Nessus安全测试工具使用教程
String -- 28. Implement strstr()
How to quickly wrap lines in Excel table
Afnetworking data raw request mode
R语言使用epiDisplay包的statStack函数基于因子变量通过分层的方式查看连续变量的统计量(均值、中位数等)以及对应的假设检验、对连续数据进行对数化之后符合参数检验条件自动执行参数检验
Solve the problem of repeated clicking of button uibutton
Matlab program for natural gas flow calculation
IEEE Transaction期刊模板使用注意事项
关于不定方程解的个数的问题
数据湖系列文章
Ansible服务常用命令模块详细解析
Mmdrawercontroller first loading sidebar height problem
Flink comprehensive case (IX)
[untitled]
Soft link, hard link
How to build and run WordPress on raspberry pie