# Broken Ptr syntax

**URL:** https://talk.remobjects.com/t/broken-ptr-syntax/25611
**Category:** Mercury
**Created:** [October 29, 2021, 2:03pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611 "2021-10-29T14:03:34Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [October 29, 2021, 2:03pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/1 "2021-10-29T14:03:34Z")

</div>

Testing the functionality of pointers, the syntax given in your documentation fails to compile:

Dim t As Ptr(Of String)  
t = AddressOf “Some Literal String”

The error is that t is expecting a ^String.

Also is it possible to extend the Item property of the Ptr class to index pointer arrays?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 29, 2021, 3:00pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/2 "2021-10-29T15:00:07Z")

</div>

> [@sollapse](#):
>
> Dim t As Ptr(Of String)  
> t = AddressOf “Some Literal String”
> 
> The error is that t is expecting a ^String.

Curious. it does not work with literals, but it does work with a String variable. This could be s-designed, and a side effect of how the constant string literal does not exist as an actual `String` class instance at tis stage. But i’ll bring it up with the team for review.

this works:

```auto
    Dim t As Ptr(Of String)
    't = AddressOf “Some Literal String”    
    Dim s = “Some Literal String”
    t = AddressOf s

```

FWIW, the same happens in C#

```auto
		string s;
		string* x = &s;
		string* y = "text"; // E62 Type mismatch, cannot assign "String" to "String*"

```

Oddly tough, in mercury this copmpiles _without_ enablig Unsafe code; in C# it doesn’t i’ll log this too.

> [@sollapse](#):
>
> Also is it possible to extend the Item property of the Ptr class to index pointer arrays?

Good idea, yes.

---

<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: [October 29, 2021, 3:02pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/3 "2021-10-29T15:02:55Z")

</div>

Logged as [bugs://E25472](https://www.remobjects.com/portal/bugs/?id=E25472). for pointers of string literals

---

<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: [October 29, 2021, 3:04pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/4 "2021-10-29T15:04:27Z")

</div>

Logged as [bugs://E25473](https://www.remobjects.com/portal/bugs/?id=E25473). for Mercury not honoring/requiring “allow unsafe code”

---

<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: [October 29, 2021, 3:06pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/5 "2021-10-29T15:06:08Z")

</div>

Logged as [bugs://E25474](https://www.remobjects.com/portal/bugs/?id=E25474). for the pointer arrays

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [October 29, 2021, 3:28pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/6 "2021-10-29T15:28:21Z")

</div>

Thanks for the response. I’m glad your team has implemented pointers in your VB framework. I created a NuGet package years ago called VBUnmanaged. I had a similar concept in mind with a Ptr class. Are delegate function pointers functional at the moment in both directions?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 29, 2021, 4:26pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/7 "2021-10-29T16:26:04Z")

</div>

> [@sollapse](#):
>
> Thanks for the response. I’m glad your team has implemented pointers in your VB framework. I created a NuGet package years ago called VBUnmanaged. I had a similar concept in mind with a Ptr class.

Cool.

> [@sollapse](#):
>
> Are delegate function pointers functional at the moment in both directions?

I’m not aware of any problems with them, so I’d like to say “yes”. But JIC, can you elaborate on what you mean by “both directions”?

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [October 29, 2021, 5:39pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/8 "2021-10-29T17:39:22Z")

</div>

Meaning, we are able to marshal data either to or from a native process.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 29, 2021, 6:03pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/9 "2021-10-29T18:03:14Z")

</div>

Oh, like that. i believe so, yes. P/Invoke isn’t my area of expertise, i suggest to give it a try 😉

---

<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: [October 29, 2021, 6:55pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/10 "2021-10-29T18:55:37Z")

</div>

[bugs://E25473](https://www.remobjects.com/portal/bugs/?id=E25473) was closed as fixed.

---

<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: [October 29, 2021, 7:08pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/11 "2021-10-29T19:08:11Z")

</div>

[bugs://E25474](https://www.remobjects.com/portal/bugs/?id=E25474) was closed as fixed.

---

<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: [October 29, 2021, 7:09pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/12 "2021-10-29T19:09:00Z")

</div>

> > [@sollapse](#):
> >
> > Also is it possible to extend the Item property of the Ptr class to index pointer arrays?
> 
> Good idea, yes.

Note since vb uses () for indexing, s(15) = s(15) + “” is it.

---

<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: [October 29, 2021, 7:09pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/13 "2021-10-29T19:09:39Z")

</div>

[bugs://E25474](https://www.remobjects.com/portal/bugs/?id=E25474) was reopened. \<\< this for documentation. the feature is there.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [October 29, 2021, 7:47pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/14 "2021-10-29T19:47:12Z")

</div>

FWIW, these fixes will not be in today’s upcoming build, as that’s already 90% done

---

<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: [November 2, 2021, 12:38pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/15 "2021-11-02T12:38:45Z")

</div>

[bugs://E25472](https://www.remobjects.com/portal/bugs/?id=E25472) was closed as fixed.

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [August 23, 2023, 3:49pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/17 "2023-08-23T15:49:15Z")

</div>

I was going through the pointer code some more as my focus is on native CPU features and I noticed there isn’t a straightforward manner for allocating/freeing memory in the Ptr object. A workaround to obtain the direct underlying pointer is to use a TryCast to IntPtr. However, their isn’t any ability to assign a IntPtr directly to the object, especially if you manually allocated from the heap. This should be allowed either through a constructor, along with a cast operator.

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [August 23, 2023, 4:00pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/18 "2023-08-23T16:00:15Z")

</div>

Can you give a more concrete sample lof what you want to do/what doesn’t work? You can use `malloc()` to allocate memory, but note that you do not want to allocate memory for _objects_ (class instances) yourself, as GC handles that.

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [August 23, 2023, 4:43pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/19 "2023-08-23T16:43:20Z")

</div>

For instance, I want to allocate enough bytes for a data structure in which I assign to the Ptr class to dereference. This is more so for pointers that will be passed to and from external code. I wouldn’t feel the need to use this for objects anyway as it seems there is a fair amount of control for the GC given if needed. My thoughts are along the lines of creating either a constructor that could manually allocate the bytes, or you could assign another pointer directly from malloc. I haven’t fully tested it yet, but I’m guessing malloc could be returned as a Ptr(as in void\*) and this could be assigned to Ptr(Of type)?

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [August 23, 2023, 5:04pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/20 "2023-08-23T17:04:35Z")

</div>

> [@sollapse](#):
>
> For instance, I want to allocate enough bytes for a data structure in which I assign to the Ptr class to dereference. This is more so for pointers that will be passed to and from external code.

ok, this should work fine, e,g.:

```auto
Module Program

  Sub Main(args as String())
    writeLn("The magic happens here.")

    Dim p As Ptr(Of Foo) = rtl.malloc(sizeOf(Foo))

    p.a = 5
    p.b = True
    writeLn($"p {p}")
    writeLn($"p.a {p.a}")
    writeLn($"p.b {p.b}")

  End Sub

End Module

Structure Foo

  Dim a As Int64
  Dim b As Boolean

End Structure

```

> [@sollapse](#):
>
> I wouldn’t feel the need to use this for objects anyway as it seems there is a fair amount of control for the GC given if needed.

Indeed.

> [@sollapse](#):
>
> My thoughts are along the lines of creating either a constructor that could manually allocate the bytes, or you could assign another pointer directly from malloc. I haven’t fully tested it yet, but I’m guessing malloc could be returned as a Ptr(as in void\*) and this could be assigned to Ptr(Of type)?

Correct, see above sample (which I just tested and works)

---

<div class="post-metadata">

### Author: ![sollapse](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/s/53a042/32.png) [@sollapse](https://talk.remobjects.com/u/sollapse)
#### Post date: [August 23, 2023, 5:34pm UTC](https://talk.remobjects.com/t/broken-ptr-syntax/25611/21 "2023-08-23T17:34:48Z")

</div>

That’s what I wanted to know. Thanks yet again! Could an example of this be added to the documentation as well?

[Next page](https://talk.remobjects.com/t/broken-ptr-syntax/25611.md?page=2)
