views:

83

answers:

3

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?

+1  A: 

The two processes have two different virtual address spaces. I would be absolutely horrified if one process could stomp on the values within another process without explicitly performing some sort of sharing (memory mapped files etc).

Was this an exercise in education, or is there something you're trying to achieve, and this was just an initial attempt? If it's the latter, please give us more details about what you're trying to do.

Jon Skeet
@Jon Skeet This is an exercise. but will this work in C++?
Adeel
No, it won't work in C++. See my answer. The OS will prevent this code from working. If you run MS-DOS, though, it will work.
Dave Markle
This doesn't work in MS-Dos either. Mostly because it cannot run more than one program at the time. Not even in TSRs, they keep their own stack.
Hans Passant
+1  A: 

Well, for one thing, memory is isolated between instances. This wasn't true in the days of MS-DOS, but nowadays, it's the "prime directive" of every modern OS.

So you'll never be able to communicate data across instances in this way.

For another thing, the memory allocator does not guarantee that it will allocate memory in the same place once it's called -- far from it. My advice is to stay away from hardcoded addresses.

And for a bit of perspective here... It seems like you need to learn a lot of fundamentals about the OS, the CLR and memory management. To me, that means you should not be touching the "unsafe" construct. You're playing with fire. It's an advanced construct, primarily made for interoperability with older codebases. My advice is to stay away from it.

Dave Markle
Nothing wrong with learning by playing with stuff, like he said, it's just an exercise.
RSlaughter
A: 

The cause is that you cannot access another process' memory so easily. That's called 'Virtual memory' and it's the way modern OSes protect running processes' memory from being damaged.

Punkoff