# Possibility of stackallocated Classes

**URL:** https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816
**Category:** Oxygene
**Tags:** island
**Created:** [December 23, 2020, 10:02pm UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816 "2020-12-23T22:02:05Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![RemObjectsSoftware](https://talk.remobjects.com/user_avatar/talk.remobjects.com/remobjectssoftware/32/15947_2.png) [@RemObjectsSoftware](https://talk.remobjects.com/u/RemObjectsSoftware)
#### Post date: [December 30, 2020, 8:03am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/22 "2020-12-30T08:03:40Z")

</div>

Thanks, logged as [bugs://85362](https://www.remobjects.com/portal/bugs/?id=85362) (subclassing records)

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 8:15am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/23 "2020-12-30T08:15:37Z")

</div>

Note that 1 thing will not work when it comes to records:

- Virtual/override. There is no real VMT to speak of, so virtual calls don’t work.

When it comes to interfaces you can always reimplement them of course.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 8:24am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/24 "2020-12-30T08:24:45Z")

</div>

> [@ck](#):
>
> Note that 1 thing will not work when it comes to records:
> 
> - Virtual/override. There is no real VMT to speak of, so virtual calls don’t work.
> 
> When it comes to interfaces you can always reimplement them of course.

So considering this:

```auto
type
   TPoint = record
   public
     x,y : float;
     method Multiply(const point: TPoint) : TPoint; virtual;
     begin
        //proper calculation here...       
     end;
  end;
 
 TMyDifferentPoint = record(TPoint)
 public
    method Multiply(const point: TPoint) : TPoint; virtual; override;
    begin
      //wont work? 
    end;
 end;

```

So the user then needs to replace this with an Interface, lets call it _IPointOperations_ which then has the “Multiply, Add, Subtract” etc…

but im actually curiours, how does the freepascal compiler handle their old-style-objects then, since they can do abstract and virtual stuff but do not allow on the other hand for interfaces haha, can you help me understanding those differents, aswell as for here, I ask out of cluelessness 😃

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 8:26am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/25 "2020-12-30T08:26:17Z")

</div>

Old style objects actually have a VMT; they’re also meant to be called virtually. They also have a vmt pointer at the start iirc.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 8:27am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/26 "2020-12-30T08:27:07Z")

</div>

Lol, but why then they cannot implement interfaces, interfaces require a VMT aswell, dont they?

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 8:28am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/27 "2020-12-30T08:28:42Z")

</div>

Interfaces were introduced _long_ after objects, and really were only done (in Delphi) to support COM initially. They are only supported on classes.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 8:30am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/28 "2020-12-30T08:30:26Z")

</div>

> [@ck](#):
>
> They

ahh yea I remember, so completly different architecture. Dont worry, I can replace then the override stuff with proper interfaces, and handle it like that, to replace the virtual stuff. Actually if you say that they do not have a VMT, it think makes records more lightweight then, do they?

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 8:31am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/29 "2020-12-30T08:31:53Z")

</div>

Oke so it’s not _entirely_ true they don’t have a vmt. They have one to support interfaces of course. What they’re missing is what classes have at [0], which is a pointer to that same VMT. Records are always purely the data they define, while classes/objects also have a vmt pointer.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 8:34am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/30 "2020-12-30T08:34:02Z")

</div>

Depends how much of a complexity this would be for you, but another point popped in my mind, since records then are not able to allow for overrides/Virtual methods, would it not be then possible to overcome the “Diamond Problem” C++ has with _Muitiple Inheritance_? Since this only arises when dealing on subclasses overrding the virtual stuff.

And for the usecase, it would just be more flexiable, again not smth REALLY must have but could allow for abit easier records inheritance-models since u do not have this diamond problem to face with,.

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 8:35am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/31 "2020-12-30T08:35:26Z")

</div>

iirc the diamond problem is also when subclassing the same tree, through different paths.

ie:  
a has int X;  
b and c descend from a.

d descends from b and c, now has two 'X’s.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 8:36am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/32 "2020-12-30T08:36:48Z")

</div>

true but this could fell away I think if you just ignore one of the values, and so to speak say (_yea I know it has both, but its duplicated, since its duplicated, we can simply reducec it to one field/property, whatever)_

Like we ignore the fact it has 2 or even more “int X” fields, and we take only the one from the root, lets say.

The only reason I came up with this now is since records are way more lightweight than classes due to their memory-layout and location(stack) the MultiInheritance should not cause memoryissues (this is in C++ always to be carefully thoughtout since they are mostly used for large heapallocs _(ateast in the contexts I have witnessed)_ and 2. they do not deal with the virtual/abstract problems you usually have with multi-class-inheritance, so its something you may take into consideration, but again, its just brainstorming, not something I would die for to have it haha

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 8:45am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/33 "2020-12-30T08:45:14Z")

</div>

I have an almost working version of struct subclassing with interface support.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 8:46am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/34 "2020-12-30T08:46:31Z")

</div>

xD not bad my friend, I knew that you guys know what you are doing butt not in that pace haha 😃

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 9:32am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/35 "2020-12-30T09:32:29Z")

</div>

It’s done. The next build has this feature. (The issue will stay open till it’s documented)

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 9:54am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/36 "2020-12-30T09:54:51Z")

</div>

Carlo I have to appologize, i was abit to quick on this one here:

> [@ck](#):
>
> constructor Move(var aInput: ^InputType);

It should leave the source intact, (aInput in this case) since it must destroy the resources it points to first then gets destroyed (maybe read this article here: I didnt perfectly explained it, my bad,… [c++ - What is move semantics? - Stack Overflow](https://stackoverflow.com/questions/3106110/what-is-move-semantics) _(especially the accepted answer!)_

but I think in C++ it is that the temporary object is being destroyed immediately after the line reaches the “;” idk if this is the case for island/cocoa compiler too, if this is the case, better do not discard the “aInput” directly, since it may cause a crash, when it sees that the value was nil b4 and then tries to destroy itt again, idk now …

but maybe just do in the move-ctor: this

```auto
constructor Move(var aInput: ^InputType);
begin
   self.data := aInput^.Data; //the pointer to some heapallocateed stuff
   aInput^.Data := nil;
  aInput := nil; //this line im not sure, sicnce in C++ that guy from forum said this is wrong behaviour, maybe here not?
end;

```

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 9:57am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/37 "2020-12-30T09:57:03Z")

</div>

Ah so it’s still “valid” (as in, the finalizer will still get called), but the compiler knows it’s the "last’ use of the value.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 9:57am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/38 "2020-12-30T09:57:38Z")

</div>

yea i think this is how it would be! Wasnt quiet sure on that myself^^

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 9:59am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/39 "2020-12-30T09:59:48Z")

</div>

Im wondering tho, why would a, if u want to call it like that, safe extra “aInput := nil” cause a crash, the compiler in that case would just skip the line if he sees no use for it, or am I wrong? Since its about to be destroyed anyway?

---

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [December 30, 2020, 10:04am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/40 "2020-12-30T10:04:21Z")

</div>

I’m going to see if I can easily catch this situation (but not today), and what semantics I’ll end up with.

---

<div class="post-metadata">

### Author: ![LazarusRisen](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@LazarusRisen](https://talk.remobjects.com/u/LazarusRisen)
#### Post date: [December 30, 2020, 10:07am UTC](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816/41 "2020-12-30T10:07:48Z")

</div>

Dw mate, Take your time for this, this maybe need to be carefully thought more precise so no hurry, im thankful you guys are considering such stuff anyway, this makes me more motivated to use it (_which obvuiously doesnt mean to accept every crap of a feature_) 😃

But I was sure on the record inhertance and movesemantics, that they will enhance Islands power in a lot of cases, when you need to be less memory hungry, call it like this haha.

All in all, have a good rest mate and I will check your reply on the headerimporter in a minute, since you are correct, i messed around with the targets, thx for the hint!

ah and btw, if you need more insight for this move-stuff, i wanna suggest really you to have a closer look into his answer, it was really well formulated out to grasp the idea of move-ctor/semantics!

[Previous page](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816.md?page=1)

[Next page](https://talk.remobjects.com/t/possibility-of-stackallocated-classes/23816.md?page=3)
