I am posting a code.
using System;
using System.Runtime.InteropServices;
class TestPointer
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
unsafe
{
int t = 8;
int* p = &t;
IntPtr addr = (IntPtr)p;
Console.WriteLine(addr.ToString("x"));
Console.WriteLine("before: " +(*p));
Console.ReadLine();
Console.WriteLine("after: " + (*p));
}
}
else
{
unsafe
{
string str = args[0];
GCHandle handle = GCHandle.Alloc(str, GCHandleType.Pinned);
IntPtr pointer = GCHandle.ToIntPtr(handle);
int* p = (int*)pointer;
int t = 5;
p = &t;
Console.WriteLine((*p));
}
}
}
}
i have run this code in two instances.
in instance1 I called as TestPointer.exe, the instance1 show memory location of 8 and than execuation stopped at Console.ReadLine() statement. On next step i run another instance (instance2) with TestPointer.exe 12f470(the memory address shown from instance1) so in this case i am changing value from 8 to 5 and after ReadLine from instance1 should show value 5 but it is still displaying 8. what is the reason?