Broken Ptr syntax

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?

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:

    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#

		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.

Good idea, yes.

1 Like

Logged as bugs://E25472. for pointers of string literals

Logged as bugs://E25473. for Mercury not honoring/requiring “allow unsafe code”

Logged as bugs://E25474. for the pointer arrays

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?

Cool.

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”?

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

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

bugs://E25473 was closed as fixed.

bugs://E25474 was closed as fixed.

Good idea, yes.

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

1 Like

bugs://E25474 was reopened. << this for documentation. the feature is there.

1 Like

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

bugs://E25472 was closed as fixed.

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.

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.

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)?

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

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

Indeed.

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

1 Like

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