<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">I would like to know the correct way to get the memory address of value types and reference types.</div><div class=""><br class=""></div><div class="">Given below are the attempts made.</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">class<span style="color: #000000" class=""> C {}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">struct<span style="color: #000000" class=""> S {}</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> c1 = <span style="color: #4f8187" class="">C</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">var</span> s1 = <span style="color: #4f8187" class="">S</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">var</span> s2 = <span style="color: #4f8187" class="">s1</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(62, 30, 129); background-color: rgb(255, 255, 255);" class=""><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">//Reference type:</div><div style="margin: 0px; line-height: normal;" class="">print<span style="color: #000000" class="">(</span><span style="color: #d12f1b" class="">"c1 address: </span><span style="color: #000000" class="">\</span><span style="color: #d12f1b" class="">(</span><span style="color: #703daa" class="">Unmanaged</span><span style="color: #000000" class="">.</span>passUnretained<span style="color: #000000" class="">(</span><span style="color: #4f8187" class="">c1</span><span style="color: #000000" class="">).</span>toOpaque<span style="color: #000000" class="">()</span><span style="color: #d12f1b" class="">)"</span><span style="color: #000000" class="">) </span><span style="color: #008400" class="">//Is this correct ?</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">//Value type:</div><div style="margin: 0px; line-height: normal;" class="">withUnsafeMutablePointer<span style="color: #000000" class="">(to: &</span><span style="color: #4f8187" class="">s1</span><span style="color: #000000" class="">) {</span></div><div style="margin: 0px; line-height: normal; color: rgb(209, 47, 27);" class=""><span style="color: #000000" class=""> </span><span style="color: #3e1e81" class="">print</span><span style="color: #000000" class="">(</span>"s1 address: <span style="color: #000000" class="">\</span>(<span style="color: #000000" class="">$0</span>)"<span style="color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class="">}</div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: Helvetica; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">withUnsafeMutablePointer<span style="color: #000000" class="">(to: &</span><span style="color: #4f8187" class="">s1</span><span style="color: #000000" class="">) {</span></div><div style="margin: 0px; line-height: normal; color: rgb(209, 47, 27);" class=""><span style="color: #000000" class=""> </span><span style="color: #3e1e81" class="">print</span><span style="color: #000000" class="">(</span>"s1 address: <span style="color: #000000" class="">\</span>(<span style="color: #000000" class="">$0</span>)"<span style="color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class="">}</div><div class=""><br class=""></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">//Output:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">//c1 address: 0x000060c0000015f0</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">//s1 address: 0x0000000119027a90</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">//s1 address: 0x0000000114571880</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class="">The address of s1 seems to be different the 2 times it is printed. It seems like my approach isn’t correct.</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><b class="">Tested on:</b></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font color="#0433ff" class="">Swift 4 </font></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><font color="#0433ff" class="">Xcode 9.0 beta 6 (9M214v)</font></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><b class="">Questions:</b></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class="">1. Is the above shown the correct way to get <font color="#0433ff" class="">reference type</font> memory address ? If not could you let me know the correct way.</div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">2. What Is it the correct way to get <font color="#0433ff" class="">value type</font> memory address ?</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">3. Is it possible to get the memory address of immutable value type (declared as </span><span style="color: rgb(186, 45, 162); font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">let</span><span style="background-color: rgb(255, 255, 255);" class="">)</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">Thanks and regards,</span></div><div class=""><span style="background-color: rgb(255, 255, 255);" class="">Muthu</span></div></body></html>