{"id":3252,"date":"2017-11-02T12:49:00","date_gmt":"2017-11-02T11:49:00","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=3252"},"modified":"2017-11-02T12:49:00","modified_gmt":"2017-11-02T11:49:00","slug":"c-guns-addressen-von-referenzen","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=3252","title":{"rendered":"C++ Guns - Addressen von Referenzen"},"content":{"rendered":"<p>Haben Referenzen in C++ eigene Adressen, so wie normale Variablen und Pointer, oder haben sie nur die Adresse der referenzierten Variablen?<br \/>\nWenn Referenzen eigene Adressen besitzen, existieren sie wie normale Variablen im RAM. Ich meine mich aber erinnert zu haben, dass eine Referenz nur ein Alias f\u00fcr eine andere Variable ist. Einfach nur syntactic sugar f\u00fcr den Programmierer. Das w\u00e4re nat\u00fcrlich optimal, da unn\u00f6tige Kopieraktionen entfallen.<br \/>\nEin kleines Experiment bringt Klarheit:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid func(int&amp; c) {\r\n  cout &lt;&lt; &quot;In func(): Adress of c reference to a: &quot; &lt;&lt; addressof(c) &lt;&lt; &quot;\\n&quot;;\r\n  c = 1;\r\n}\r\n\r\nint main() {\r\n  int a = 0;\r\n  cout &lt;&lt; &quot;Value of a: &quot; &lt;&lt; a &lt;&lt; &quot;\\n&quot;;\r\n  cout &lt;&lt; &quot;Adress of a: &quot; &lt;&lt; addressof(a) &lt;&lt; &quot;\\n&quot;;\r\n  int &amp;b = a;\r\n  cout &lt;&lt; &quot;Adress of b reference to a: &quot; &lt;&lt; addressof(b) &lt;&lt; &quot;\\n&quot;;\r\n  func(a);\r\n  cout &lt;&lt; &quot;Value of a: &quot; &lt;&lt; a &lt;&lt; &quot;\\n&quot;;\r\n}\r\n<\/pre>\n<pre>\r\nValue of a: 0                                                                                                                                                                       \r\nAdress of a: 0x7ffd58cf22b4                                                                                                                                                         \r\nAdress of b reference to a: 0x7ffd58cf22b4                                                                                                                                          \r\nIn func(): Adress of c reference to a: 0x7ffd58cf22b4                                                                                                                               \r\nValue of a: 1\r\n<\/pre>\n<p>Es gibt nur eine einzige Variable im Programm, mit der Adresse 0x7ffd58cf22b4. Es ist egal, ob \u00fcber eine Referenz darauf zugegriffen wird, oder sogar aus einer Funktion heraus, \u00fcber eine Referenz, die Variable a manipuliert wird. Es werden nur 4Byte Speicher im RAM belegt. Wunderbar.<\/p>\n<p>Mit Pointer sollte dieser Effekt nicht zu erreichen sein. Da Pointer ja echte Variablen mit eigenen Adressen. <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid func(int* c) {\r\n  cout &lt;&lt; &quot;In func(): Adress of c pointer to a: &quot; &lt;&lt; addressof(c) &lt;&lt; &quot;\\n&quot;;\r\n  *c = 1;\r\n}\r\n\r\nint main() {\r\n  int a = 0;\r\n  cout &lt;&lt; &quot;Value of a: &quot; &lt;&lt; a &lt;&lt; &quot;\\n&quot;;\r\n  cout &lt;&lt; &quot;Adress of a: &quot; &lt;&lt; addressof(a) &lt;&lt; &quot;\\n&quot;;\r\n  int* b = addressof(a);\r\n  cout &lt;&lt; &quot;Adress of b pointer to a: &quot; &lt;&lt; addressof(b) &lt;&lt; &quot;\\n&quot;;\r\n  func(addressof(a));\r\n  cout &lt;&lt; &quot;Value of a: &quot; &lt;&lt; a &lt;&lt; &quot;\\n&quot;;\r\n}\r\n<\/pre>\n<pre>\r\nValue of a: 0\r\nAdress of a: 0x7ffc955db71c\r\nAdress of b pointer to a: 0x7ffc955db710\r\nIn func(): Adress of c pointer to a: 0x7ffc955db6e8\r\nValue of a: 1\r\n<\/pre>\n<p>Ganz klar zu sehen, jeder Pointer hat eine andere Adresse als Variable a. Es werden also 3*4Byte = 12Byte Speicher im RAM belegt. Plus zus\u00e4tzliche Kopieraktionen der Adresse von a hin zur Pointervariablen. <\/p>\n<p>Interessanterweise sind Referenzen im C Sprachstandard nicht vorgesehen. Somit ist es nicht m\u00f6glich, dieses simple Beispiel auf Laufzeit hin optimiert in C zu programmieren. Nur in C++.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Haben Referenzen in C++ eigene Adressen, so wie normale Variablen und Pointer, oder haben sie nur die Adresse der referenzierten Variablen? Wenn Referenzen eigene Adressen besitzen, existieren sie wie normale Variablen im RAM. Ich meine mich aber erinnert zu haben, dass eine Referenz nur ein Alias f\u00fcr eine andere Variable ist. Einfach nur syntactic sugar [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-3252","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3252"}],"version-history":[{"count":6,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3252\/revisions"}],"predecessor-version":[{"id":3261,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3252\/revisions\/3261"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3252"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}