<?xml version='1.0' encoding='utf-8' ?>

<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/' xmlns:atom10='http://www.w3.org/2005/Atom'>
<channel>
  <title>jack</title>
  <link>https://jack.dreamwidth.org/</link>
  <description>jack - Dreamwidth Studios</description>
  <lastBuildDate>Mon, 03 Jul 2017 10:14:23 GMT</lastBuildDate>
  <generator>LiveJournal / Dreamwidth Studios</generator>
  <lj:journal>jack</lj:journal>
  <lj:journaltype>personal</lj:journaltype>
  <image>
    <url>https://v2.dreamwidth.org/5140256/61366</url>
    <title>jack</title>
    <link>https://jack.dreamwidth.org/</link>
    <width>100</width>
    <height>41</height>
  </image>

<item>
  <guid isPermaLink='true'>https://jack.dreamwidth.org/1037020.html</guid>
  <pubDate>Mon, 03 Jul 2017 10:14:23 GMT</pubDate>
  <title>Pointer-to-owner: rust, java, and other languages</title>
  <link>https://jack.dreamwidth.org/1037020.html</link>
  <description>I previously talked about accessing the scope of an owning class from a class declared and instantiated within it. Latest post here: &lt;a href=&quot;https://jack.dreamwidth.org/1017241.html&quot;&gt;https://jack.dreamwidth.org/1017241.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The possible approaches seem to be: the owned class has a pointer back to the owning class; or functions dealing with the owned class get an extra parameter to the owning class. Whether that&apos;s implemented manually, or automatically by the language, or somewhere between.&lt;br /&gt;&lt;br /&gt;Thinking some more about this, various things occurred to me:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Java&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;I hadn&apos;t realised, but I learned Java and C# (and maybe other recent/managed languages) do this automatically, presumably implemented by the owned class automatically having a pointer to the owning class, and checked where its instantiated that it&apos;s only instantiated by an instance of the owning class.&lt;br /&gt;&lt;br /&gt;&lt;rust&gt;&lt;br /&gt;&lt;br /&gt;I was naturally drawn to a &quot;owning pointer is passed in alongside or as part of a this pointer&quot; implementation as it seemed more conceptually correct. However, the actual benefit of this is a lot smaller in most languages other than rust. I first started thinking about these options in a rust example, where having a pointer to the owning class needed some fancy dancing, because rust prefers to keep tight limits on how many pointers to a class at once (ideally one only).&lt;br /&gt;&lt;br /&gt;This hopefully makes memory management safer, and means you can usually move classes around in memory using a raw memcpy, because they don&apos;t usually have internal pointers to different parts of them. But most other languages don&apos;t even try to do that, just assume that a non-trivial class is fixed in place in memory (or moved only by a garbage collector that knows where all the pointers are).&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Implementation&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;If you try to avoid having a permanent pointer back to the owning class, and if you ever need a pointer to the owned class (this is common if you use it as a callback), you need to accept your pointers would actually be a pair (or more) of pointers, to the owning class, and to the owned class. The owned pointers might be an offset rather than a complete pointer. That&apos;s clunky, but wouldn&apos;t necessarily take up that much space if the language supports it. You could do a similar thing for iterators, like pointers to members of a collection, rather than having a bare pointer that only makes sense if you already know what the collection is.&lt;br /&gt;&lt;br /&gt;That seems a useful concept, but I&apos;m not sure how useful it would be in practice.&lt;/rust&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;https://www.dreamwidth.org/tools/commentcount?user=jack&amp;ditemid=1037020&quot; width=&quot;30&quot; height=&quot;12&quot; alt=&quot;comment count unavailable&quot; style=&quot;vertical-align: middle;&quot;/&gt; comments</description>
  <comments>https://jack.dreamwidth.org/1037020.html</comments>
  <category>rust</category>
  <category>java</category>
  <category>tech</category>
  <lj:security>public</lj:security>
  <lj:reply-count>6</lj:reply-count>
</item>
</channel>
</rss>
