A question about functionality in this code snippet


can please answer few questions regarding example? questions below example.

unsafe static void testmethod()  {      // assume following class exists.       //class point        //{        //    public int x;       //    public int y;        //}         // variable pt managed variable,      // subject garbage collection.      point pt = new point();        // using fixed allows address of pt members taken,       // , "pins" pt not relocated.        fixed (int* p = &pt.x)      {          *p = 1;      }  }

questions:

  1. what "&" operator directly before "pt.x" in case (i know it's typically , operator)?
  2. what significance of asterisk (*) being after data type?
  3. what significance of asterisk (*) being before variable had asterisk after data type?

i know asterisk has memory pointer variable, not sure of significance of being after data type, before variable , how plays "fixed" statement. understand fixed statement causes variable not moved in memory.

ultimately i'm trying use system.security.securestring. example securestring uses "fixed" statement, started researching this. brought me the example above , questions. i have worked in .net for... since came out, there's new learn. i'd appreciate explanations! thanks!


nathon dalton
sr. software engineer
blog: http://nathondalton.wordpress.com

you can read single ampersand "address of". read expression "&pt.x" "the memory address @ value pt.x stored".

you can read asterisk after data type "pointer". read expression "int* p" "a variable name 'p' typed pointer integer".

you can read asterisk after variable "pointer". read expression "*p" "the pointer name 'p'".

in summary, read expression "int* p = &pt.x" "the integer pointer named 'p' should point memory address @ value pt.x stored".

read "*p = 1" expression "place value 1 @ memory address pointed pointer named 'p'".

the code has same effect writing "pt.x = 1".



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'