How to get pointer to a string stored in an array element?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
just me
Posts: 9466
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to get pointer to a string stored in an array element?

13 May 2023, 10:51

@Helgef,
interesting concept. I'm still struggling with the new objects. Finally I chose something similar:

Code: Select all

   RBArr := []
   RBBuf := Buffer(TDBSize * RBCnt, 0) ; C array of TASKDIALOG_BUTTON structures
   Addr := RBBuf.Ptr
   For I, V In Radios {
      Buf := Buffer((StrLen(V) + 1) * 2, 0)
      StrPut(V, Buf.Ptr)
      RBArr.Push(Buf)
      Addr := NumPut("Int", MinRID + I, "Ptr", Buf.Ptr, Addr)
   }
Thanks!
xroot
Posts: 40
Joined: 21 Jun 2019, 08:45

Re: How to get pointer to a string stored in an array element?

14 May 2023, 09:03

@just me
Looking @ your first example how about this:

Code: Select all

Arr := [StrPtr("Hello!"),StrPtr("Olleh!")]

;Ptr1 := StrPtr(Arr[1])
;Ptr2 := StrPtr(Arr[2])

MsgBox Arr[1] " " StrGet(Arr[1]) 
just me
Posts: 9466
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to get pointer to a string stored in an array element?

15 May 2023, 02:06

@xroot, it would work, but it would be an array of pointers not an array of strings.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: How to get pointer to a string stored in an array element?

15 May 2023, 12:42

iseahound wrote:
12 May 2023, 23:15
I think Lexikos also says that it wont be a race condition (implicitly).
[...]
Notice the use of a temporary string buffer here.
The life time of string pointers are described, explicitly, in the documentation for :arrow: strptr. In particular,
The address of a literal string is valid until the program exits. The script should not attempt to modify the string.
Cheers.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to get pointer to a string stored in an array element?

10 Jun 2023, 01:08

I think the reasons I chose to remove GetAddress entirely rather than replacing it with multiple methods (like GetPropAddress and GetItemAddress) were:
  • Further increasing cost for each type of "container" that can provide a string address. More code and documentation to write, test and maintain. More special cases for users to remember.
  • I tentatively plan to experiment with reference-counted strings, which would ideally improve performance and enable StrPtr to be used (relatively) safely with strings from any container.
  • There are efficient alternatives. While you may need a pointer to a string, you don't truly need that string to be contained by an array or property.
I think there's a stronger case for using arrays this way than properties or maps, so depending on how v2.1 develops, I might consider restoring GetAddress for arrays.

In retrospect, the capabilities previously present in SetCapacity and GetCapacity may have biased me toward removing GetAddress. Effectively duplicating the functionality of a Buffer into every property, array element and map item is somewhat less appealing than providing a simple method to efficiently utilize strings already stored in an array.
xroot
Posts: 40
Joined: 21 Jun 2019, 08:45

Re: How to get pointer to a string stored in an array element?

11 Jun 2023, 09:00

@just me
Try using a SafeArray, this might solve your strptr issue.

Code: Select all

;arr    := ["Hello!","Olleh!"]

arr    := ComObjArray(12,2)
arr[0] := "Hello!"
arr[1] := "Olleh!"

ptr1 := StrPtr(arr[0])
ptr2 := StrPtr(arr[1])

MsgBox ptr1 " " StrGet(ptr1) " " ptr2 " " StrGet(ptr2)


Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps, Bing [Bot], hisrRB57, songdg and 99 guests