.net - c# pointers vs IntPtr -


this 2nd part of 1st question using c# pointers

so pointers in c# 'unsafe' , not managed garbage collector while intptr managed object. why use pointers then? , when possible use both approaches interchangeably?

the cli distinguishes between managed , unmanaged pointers. managed pointer typed, type of pointed-to value known runtime , type-safe assignments allowed. unmanaged pointers directly usable in language supports them, c++/cli best example.

the equivalent of unmanaged pointer in c# language intptr. can freely convert pointer , forth cast. no pointer type associated though name sounds "pointer int", equivalent of void* in c/c++. using such pointer requires pinvoke, marshal class or cast managed pointer type.

some code play with:

using system; using system.runtime.interopservices;  unsafe class program {     static void main(string[] args) {         int variable = 42;         int* p = &variable;         console.writeline(*p);         intptr raw = (intptr)p;         marshal.writeint32(raw, 666);         p = (int*)raw;         console.writeline(*p);         console.readline();     } } 

note how unsafe keyword appropriate here. can call marshal.writeint64() , no complaint whatsoever. corrupts stack frame.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -