Pointers in C#
-
Samstag, 18. August 2012 17:25
Hi
I created an "unsafe" method within a class, and I used pointers to fetch the address of some variable defined in that method. I called the object of the Program class and called the method using two different objects. Now when I try to fetch that address via Visual Studio 2010 ,I get the same address every time I execute the console application, and when I use the Visual Studio command prompt and compile the code using the "/unsafe" property, I am getting a different address value.
When I try doing this in Visual Studio 2008 and windows 7, I am getting a different address value at every execution of the code. The code snippet is as
public unsafe int method()
{
int* p;
int s ;
p = &s;
return (int)p;
}
static void Main(string[] args)
{
string s = "";
Program p1 = new Program();
Program p2 = new Program();p1.method();
p2.method();
}
Can anyone explain how the Operating system is creating memory in each of the cases defined above?
Alle Antworten
-
Samstag, 18. August 2012 18:17
The memory address can be the same as you're invoking your methode in a serial manner. Thus the memory address can be reused.
-
Sonntag, 19. August 2012 16:17Yes I know but with different Operating Systems and different visual studio versions , Why am I getting different addresses and if I use visual studio 2008 i get different result at every new execution but in case of visual studio 2010, I am getting the same value again and again!!
-
Sonntag, 19. August 2012 18:02
In our experiments with Visual Studio 2010, we added ‘Console.WriteLine’ to check the results of ‘method’, and we get different results on each execution. Maybe for certain reasons, Data Execution Prevention, which seems to be related to this topic, is turned off for your executable?
How did you check the results of ‘method’ in your tests?
-
Montag, 20. August 2012 09:07The program is loaded into memory at an available place, wherever that available place is. There is no need to have that location fixed. If the programs were always loaded at a fix address, what would happen if two programs requested the same location?
-
Montag, 20. August 2012 18:20
Hi
I am sorry, I did not write the "Console.WriteLine(s)" statement and the part where I am concatenating the the two addresses and converting them into a string of 14 characters.
s= Convert.ToString(p1.method())+Convert.ToString(p2.method);
This string is getting different results in the 2008 version and same results in the 2010 version.
Thanks.
-
Montag, 20. August 2012 18:21Yes. That is the thing beyond my comprehension, in different versions I am getting different results. Please try this in visual studio 2012 and different frameworks to get a better idea.
-
Dienstag, 21. August 2012 07:01
Why do you expect the same results?
Your program is not the only one in the system. The available memory at a given time is not the same as the available memory at another time. Of course, two computers will not have the same things in memory. If they happen to have the exact same things, they won't be loaded in the same order and won't end up at the same locations.
-
Dienstag, 21. August 2012 07:11
Here a schematic view of memory and programs.
Let's suppose a simplified computer with 4 locations L1-L4 in memory. The system (S) needs one location to load. Your anti-virus (AV) needs one too. And your program (P) needs one. Whenever your anti-virus scans the disk, it allocates another location to work (W).
T1: the system starts. L1: S.
T2: the anti-virus starts. L1: S. L2: AV.
T3: the anti-virus scans the system. L1: S. L2: AV. L3: W.
T4: you start your program. L1: S. L2: AV. L3: W. L4: P.
T5: the anti-virus has finished scanning the system. L1: S. L2: AV. L3: free. L4: P.
T6: your program ends. you restart it. L1: S. L2: AV. L3: P.
Because you started your program was started at different times, it is loaded in different locations. Of course, things are a bit more complicated in real life, because you don't have only 3 things in memory and the pattern of the available memory is more complicated.
-
Dienstag, 21. August 2012 10:53
Hi Louis
I am not expecting the same results at every execution, but when I use Visual studio 2010, .NET framework 3.5 and windows XP, I am getting the same result at every execution even when I create 2 objects , I get the same value. When I use visual studio 2012 and .NET framework version 4.5 , I am getting different results, I just can't seem to understand when at every attempt of execution I should get a different value ,why Visual Studio 2010 is giving me the same values.
I know 2 different systems will give different values and also the type of Operating System will alter the values, I am expecting different values at every execution o fthe application and also the creation of different objects.
Shivi Gupta
-
Dienstag, 21. August 2012 13:50
Let's see if I understand. What disappoints you is that you're getting the same result over multiple execution while you expect them to be different?
-
Dienstag, 21. August 2012 14:36
Here a schematic view of memory and programs.
Let's suppose a simplified computer with 4 locations L1-L4 in memory. The system (S) needs one location to load. Your anti-virus (AV) needs one too. And your program (P) needs one. Whenever your anti-virus scans the disk, it allocates another location to work (W).
T1: the system starts. L1: S.
T2: the anti-virus starts. L1: S. L2: AV.
T3: the anti-virus scans the system. L1: S. L2: AV. L3: W.
T4: you start your program. L1: S. L2: AV. L3: W. L4: P.
T5: the anti-virus has finished scanning the system. L1: S. L2: AV. L3: free. L4: P.
T6: your program ends. you restart it. L1: S. L2: AV. L3: P.
Because you started your program was started at different times, it is loaded in different locations. Of course, things are a bit more complicated in real life, because you don't have only 3 things in memory and the pattern of the available memory is more complicated.
Keep in mind that the memory address being printed is an address in that programs *virtual* memory space. It's not a pointer to physical memory.
Because of this what matters is how much of the stack has been taken up by the time that method is reached. The user code will always take up a fixed amount of stack space for each run of the program, but there is some unknown (and possibly fixed or variable) amount of stack space taken up by code that runs before the use code is reached. Apparently with a particular version of C# that amount is fixed. (Or you just got really, really lucky.)
At the end of the day though; that just doesn't matter. Even when you are manually managing memory in unsafe code you don't actually need to know or care where, within each particular section of memory, a particular object is located.
- Als Antwort markiert Shivi Gupta Dienstag, 21. August 2012 16:59
-
Dienstag, 21. August 2012 17:08Thank you servy42, you have cleared all my doubts. I can end this discussion, many thanks to the rest of the members who made this incomprehensible code easy to understand.
Shivi Gupta

